在后台shell脚本使用的netcat / CAT(如何避免停止(TTY输入)?) [英] Using netcat/cat in a background shell script (How to avoid Stopped (tty input)? )

查看:1892
本文介绍了在后台shell脚本使用的netcat / CAT(如何避免停止(TTY输入)?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

摘要:如何在后台运行交互式任务

Abstract: How to run an interactive task in background?

详细信息:我想作为后台任务运行下的灰壳(Busybox的)这个简单的脚本

Details: I am trying to run this simple script under ash shell (Busybox) as a background task.

&myscript.sh放大器;

myscript.sh&

不过剧本立即停止...

However the script stops immediately...

[1] +停止(TTY输入)myscript.sh

[1]+ Stopped (tty input) myscript.sh

该myscript.sh内容...(只有relvant一部分,其他然后,我的陷阱SIGINT,SIGHUP等)

The myscript.sh contents... (only the relvant part, other then that I trap SIGINT, SIGHUP etc)

#!/bin/sh

catpid=0

START_COPY()
{
  cat /dev/charfile > /path/outfile &
  catpid = $! 
}

STOP_COPY()
{
  kill catpid 
}

netcat SOME_IP PORT | while read EVENT
do
  case $EVENT in
    start) START_COPY;;
    stop) STOP_COPY;;
  esac
done

从简单的命令行测试中,我发现,机器人猫的netcat尝试从tty的读取。
请注意,这的netcat版本没有-e苏preSS终端。

From simple command line tests I found that bot cat and netcat try to read from tty. Note that this netcat version does not have -e to supress tty.

现在可以做些什么来避免的MyScript正在停止?

Now what can be done to avoid myscript becoming stopped?

事情我都试过所以没有任何成功:

Things I have tried so for without any success:

1)的netcat /猫...&下;为/ dev / tty的(或输出的tty

1) netcat/cat ... < /dev/tty (or the output of tty)

2)运行包含猫的netcat使用子shell块()。这可能工作,但如何抓住猫PID?

2) Running the block containing cat and netcat in a subshell using (). This may work but then how to grab PID of cat?

在你的专家...

问题依然存在。
为大家简单的测试尝试:

The problem still exists. A simple test for you all to try:

1)在一个终端运行的netcat -l -p 11111(不含&安培;)

1) In one terminal run netcat -l -p 11111 (without &)

2)在另一个终端运行netcat的本地主机11111安培; (这应该停止后消息的同时,停止(TTY输入))

2) In another terminal run netcat localhost 11111 & (This should stop after a while with message Stopped (TTY input) )

如何避免这种情况?

推荐答案

它告诉它不要读你可能想的netcat的-d选项 STDIN

you probably want netcat's "-d" option, which tells it not to read from STDIN.

这篇关于在后台shell脚本使用的netcat / CAT(如何避免停止(TTY输入)?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆