Using netcat/cat in a background shell script (How to avoid Stopped (tty input)? )

自作多情 提交于 2019-11-27 02:52:28

问题


Abstract: How to run an interactive task in background?

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

myscript.sh&

However the script stops immediately...

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

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

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.

Now what can be done to avoid myscript becoming stopped?

Things I have tried so for without any success:

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

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

Over to you experts...


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

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

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

How to avoid this?


回答1:


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




回答2:


I can confirm that -d will help netcat run in the background.

I was seeing the same issue with:

nc -ulk 60001 | nc -lk 60002 &

Every time I queried the jobs, the pipe input would stop.

Changing the command to the following fixed it:

nc -ulkd 60001 | nc -lk 60002 &



回答3:


Are you sure you've given your script as is or did you just type in a rough facsimile meant to illustrate the general idea? The script in your question has many errors which should prevent it from ever running correctly, which makes me wonder.

  1. The spaces around the = in catpid=$! make the line not a valid variable assignment. If that was in your original script I am surprised you were not getting any errors.

  2. The kill catpid line should fail because the literal word catpid is not a valid job id. You probably want kill "$catpid".

As for your actual question:

  • cat should be reading from /dev/charfile and not from stdin or anywhere else. Are you sure it was attempting to read tty input?

  • Have you tried redirecting netcat's input like netcat < /dev/null if you don't need netcat to read anything?




回答4:


I have to use a netcat that doesn't have the -d option.

"echo -n | netcat ... &" seems to be an effective workaround: i.e. close the standard input to netcat immediately if you don't need to use it.




回答5:


As it was not yet really answered, if using Busybox and -d option is not available, the following command will keep netcat "alive" when sent to background:

tail -f /dev/null | netcat ...

netcat < /dev/null and echo -n | netcat did not work for me.



来源:https://stackoverflow.com/questions/7042375/using-netcat-cat-in-a-background-shell-script-how-to-avoid-stopped-tty-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!