Running piped bash script in background
Solution 1
Sounds like it's reading from standard input (stdin). Try adding the -d
(Do not attempt to read from stdin) option to netcat
. Or redirect on the command line:
netcat localhost 1099 <&- | bash loop.sh &
You may also want to use nohup
to make sure that it won't be adversely affected when/if you exit that shell before it stops.
Solution 2
The netcat process is reading from its standard input. Since it's connected to the terminal, the process group that netcat is in (i.e. the pipeline) must run in the foreground.
You need to plug something at the beginning of the pipeline that will send the input that the server expects. For example, with an HTTP server, you might have something like
netcat localhost 1099 <<EOF | … &
GET HTTP/1.0 http://localhost:1099/somewhere
X-Header: wibble
EOF
If the server doesn't read any input, redirect netcat
's input from /dev/null
.
Solution 3
Have discovered an utility that promises to provide pipes without the need for a separate new daemonized bash script. May come in handy: https://github.com/flonatel/pipexec
pipexec -- [NETCAT netcat localhost 1099] [LOOP bash loop.sh] '{NETCAT:1>LOOP:0}'
Related videos on Youtube
Andrew Munro
Updated on September 18, 2022Comments
-
Andrew Munro almost 2 years
I'm attempting to build a monitoring script to watch localhost communication using netcat. I have two scripts that I've built, one to start the monitoring loop and one for the loop itself. They are as follows :
start.sh
#!/bin/bash netcat localhost 1099 | bash loop.sh &
loop.sh
#!/bin/bash while read sInput; do ...do something with $sInput done
Simply running this application without in the foreground works fine but as soon as I try to run this script in the background it goes from running to stopped instantly. Could someone please educate me as to why this is happening and how to alleviate the problem.
My end goal is to have a bash script that I can create an arch linux daemon script with and have everything work perfectly. Your help is appreciated.
-
Andrew Munro over 12 yearsI understand your explanation but not the solution. I should mention that I'm only using netcat to read and not write. In this case, could I simply use the -d option to inhibit from using stdin?
-
Gilles 'SO- stop being evil' over 12 years@munroan Yes, any solution that disconnects
netcat
from the terminal is good. Arcege and I provided different ways of doing this. -
Andrew Munro over 12 yearsThanks so much . With both of your comments I came up with this... netcat -d localhost 1099 | bash loop.sh& >/dev/null However I have one remaining issue. Despite the fact that I redirect output to null, my loop.sh script still echoes to console. Any ideas why?
-
Gilles 'SO- stop being evil' over 12 years@munroan You need to put the redirection before the
&
. What you wrote runs… | bash loop.sh
in the background, then runs the command>/dev/null
(i.e. the empty command, which does nothing, with its output redirected to/dev/null
). -
Andrew Munro over 12 yearsThanks so much for your help. For accuracy I have to give it to Arcege.