shell gurus,
I have a bash shell script, in which I launch a background function, say foo()
, to display a progress bar for a boring and long command:
foo() { while [ 1 ] do #massively cool progress bar display code sleep 1 done } foo & foo_pid=$! boring_and_long_command kill $foo_pid >/dev/null 2>&1 sleep 10
now, when foo
dies, I see the following text:
/home/user/script: line XXX: 30290 Killed foo
This totally destroys the awesomeness of my, otherwise massively cool, progress bar display.
How do I get rid of this message?
Killing a background process is fairly straightforward; use the command pkill and the process ID, or process name as: Using the pkill command will force terminate (-9) the processes with the process name of ping.
pkill. pkill is one of the commands that can directly kill a background process, using the process name.
To cancel a background job, use the kill command. To be able to kill a process, you must own it. (The superuser, however, can kill any process except init.) Before you can cancel a background job, you need to know either a PID, job identifier, or PGID.
kill $foo_pid wait $foo_pid 2>/dev/null
BTW, I don't know about your massively cool progress bar, but have you seen Pipe Viewer (pv)? http://www.ivarch.com/programs/pv.shtml
Just came across this myself, and realised "disown" is what we are looking for.
foo & foo_pid=$! disown boring_and_long_command kill $foo_pid sleep 10
The death message is being printed because the process is still in the shells list of watched "jobs". The disown command will remove the most recently spawned process from this list so that no debug message will be generated when it is killed, even with SIGKILL (-9).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With