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

Summary: How to run an interactive task in the background?

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

myscript.sh &

However, the script stops immediately ...

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

The contents of myscript.sh ... (only part of the relvant, except that I hook on 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 cat and netcat are trying to read from tty. Note that this version of netcat does not have -e to suppress tty.

Now what can be done to avoid myscript stopping?

Things I tried so without success:

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

2) Running a block containing cat and netcat in a subshell using (). It might work, but then how to capture the cat's PID?

For you, experts ...


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

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

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

How to avoid this?

+7
source share
5 answers

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

+9
source

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

I saw the same problem:

 nc -ulk 60001 | nc -lk 60002 & 

Each time I requested jobs , the entrance to the channel stopped.

Changing the command to the following has been fixed:

 nc -ulkd 60001 | nc -lk 60002 & 
+6
source

Are you sure you gave your script as is, or did you just type in a rough facsimile to illustrate the general idea? The script in your question has many errors that should prevent it from working incorrectly, which makes me wonder.

  • The spaces around = in catpid=$! invalidates a string. If it was in your original script, I am surprised that you did not receive any errors.

  • The kill catpid should fail because the literal word catpid not a valid job identifier. You probably want to kill "$catpid" .

Regarding your current question:

  • cat should be read from /dev/charfile , not from stdin or anywhere else. Are you sure you tried to read the tty input?

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

+4
source

I need to use netcat, which does not have the -d option.

"echo -n | netcat ... &" seems to be an effective way: for example, close standard input to netcat immediately if you don't need to use it.

+1
source

Since this question has not yet been answered if the Busybox and -d options are not available, the following command will keep netcat alive when sent to the background:

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

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

0
source

All Articles