tags:

views:

1298

answers:

3

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?

Thanks

+2  A: 

Maybe detach the process from the current shell process by calling 'disown'?

Matthias Kestenholz
+3  A: 

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.

wnoise
I launched a background process in a shell script. When kill it, I get the 'Terminated' message.I don't quiete understand what you mean by redirecting stderr temporary in a subshell. Doesn't this mean that it will not affect the script as it's being done in a subshell? Thus not work in my script?
+1  A: 

+1 for using disown. It may not seem like a 'good' solution, but it works surprisingly well, and avoids setting/unsetting modes.

paulw1128