Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to suppress Terminated message after killing in bash?

Tags:

bash

shell

unix

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?

like image 939
user14437 Avatar asked Sep 17 '08 09:09

user14437


1 Answers

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.

like image 172
Mark Edgar Avatar answered Sep 22 '22 18:09

Mark Edgar