How can you suppress the
Terminated message that comes up after you kill a
process in a bash script?
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?
The short answer is that you can't. Bash always prints the status of foreground jobs. The monitoring flag only applies for background jobs, and only for interactive shells, not scripts.
see notify_of_job_status() in jobs.c.
As you say, you can redirect so standard error is pointing to /dev/null but then you miss any other error messages. You can make it temporary by doing the redirection in a subshell which runs the script. This leaves the original environment alone.
(script 2> /dev/null)
which will lose all error messages, but just from that script, not from anything else run in that shell.
You can save and restore standard error, by redirecting a new filedescriptor to point there:
exec 3>&2 # 3 is now a copy of 2 exec 2> /dev/null # 2 now points to /dev/null script # run script with redirected stderr exec 2>&3 # restore stderr to saved exec 3>&- # close saved version
But I wouldn't recommend this -- the only upside from the first one is that it saves a sub-shell invocation, while being more complicated and, possibly even altering the behavior of the script, if the script alters file descriptors.
For more appropriate answer check answer given by Mark Edgar
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
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.