在后台 shell 脚本中使用 netcat/cat(如何避免停止(tty 输入)?) [英] Using netcat/cat in a background shell script (How to avoid Stopped (tty input)? )
问题描述
摘要:如何在后台运行交互式任务?
Abstract: How to run an interactive task in background?
详细信息:我正在尝试在 ash shell (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 内容...(仅相关部分,除此之外我捕获 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
通过简单的命令行测试,我发现 bot cat 和 netcat 尝试从 tty 读取.请注意,此 netcat 版本没有 -e 来抑制 tty.
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/cat ...
/dev/tty(或 tty
的输出)
1) netcat/cat ... < /dev/tty (or the output of tty
)
2) 使用 () 在子 shell 中运行包含 cat 和 netcat 的块.这可能有效,但是如何获取猫的 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 localhost 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屋!