Reputation: 383
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?
Upvotes: 13
Views: 33156
Reputation: 11
Combining screen
and disown
-ing process work for me, as '-d' option is not a valid anymore for netcat. Tried redirecting like nc </dev/null
but session ends prematurely (as I need -q 1 to make sure nc process stop as file transfer finished)
Setup Receiver side first,
on Receiver side, screen keep stdin for netcat so it won't terminated
EDIT: I was wrong, you need to enter command INSIDE screen
. You'll end with no file saved, or weird binary thing flow in your terminal while attach to screen
, if you redirecting nc
inline of screen
command. (Example, this is THE WRONG WAY: )screen nc -l -p <listen port> -q 1 > /path/to/yourfile.bin
screen
, then press return/Enter
on welcome message. new blank shell will appear (you're inside screen now)type command: nc -l -p 1234 > /path/to/yourfile.bin
then, press CTRL + a , then press d
to detach screen.
on Sender sides, disown process, quit 1s after reaching EOF
cat /path/to/yourfile.bin | nc -q1 100.10.10.10 1234 & disown
Upvotes: 0
Reputation: 1
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.
Upvotes: 0
Reputation: 826
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.
Upvotes: 2
Reputation: 101
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 &
Upvotes: 9
Reputation: 141
you probably want netcat's "-d" option
, which tells it not to read from STDIN
.
Upvotes: 14
Reputation: 1808
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.
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.
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?
Upvotes: 4