How can you suppress the Terminated
message that comes up after you kill a process in a bash script?
I tried set +bm
, but that doesn't work.
I know another solution involves calling exec 2> /dev/null
, but is that reliable? How do I reset it back so that I can continue to see stderr?
In order to silence the message, you must be redirecting stderr
at the time the message is generated. Because the kill
command sends a signal and doesn't wait for the target process to respond, redirecting stderr
of the kill
command does you no good. The bash builtin wait
was made specifically for this purpose.
Here is very simple example that kills the most recent background command. (Learn more about $! here.)
kill $! wait $! 2>/dev/null
Because both kill
and wait
accept multiple pids, you can also do batch kills. Here is an example that kills all background processes (of the current process/script of course).
kill $(jobs -rp) wait $(jobs -rp) 2>/dev/null
I was led here from bash: silently kill background function process.
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