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

空扰寡人 提交于 2019-11-28 10:13:36
darkuncle

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

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 &

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?

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.

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.

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