2
When killing a process with kill -9 $PID &>/dev/null
in a script, a message is still printed to the terminal after the next command terminates. How do you stop this behaviour?
For example
while true; do
/usr/bin/dostuff -a -b -c
PID=$(pidof -o %PPID /usr/bin/dostuff)
sleep 1;
kill -KILL $PID &>/dev/null
echo "hello"
done
will print something like
hello
./my-cript.sh: line 12: 7134 Killed
/usr/bin/dostuff -a -b -c
When I only want it to print "hello"
EDIT: The clean solution is to either run the program in a subshell, or disown it.
#SOLUTION
while true; do
/usr/bin/dostuff -a -b -c &
disown
PID=$!
sleep 1;
kill -KILL $PID &>/dev/null
echo "hello"
done
1
See: http://speculation.org/garrick/kill-9.html or http://aplawrence.com/SCOFAQ/FAQ_scotec6killminus9.html or http://sial.org/howto/shell/kill-9/
– Paused until further notice. – 2010-02-18T02:03:55.413Why are you even running kill in the background. In the normal case, kill will return quickly and not need to be backgrounded. In this case, you are sleeping anyway...so the what's the point? Just remove the '&'. – William Pursell – 2010-02-18T11:40:24.273
1kill isn't being run in the background.
&>
redirects stdin and stdout – brice – 2010-02-18T15:26:34.153