How to suppress a completed message after a kill in bash?

How can you suppress the Terminated message that appears after you kill a process in a bash script?

I tried set +bm but this does not work.

I know another solution involves calling exec 2> /dev/null , but is it reliable? How can I return reset so that I can continue to see stderr?

+50
unix bash shell
Sep 17 '08 at 9:40
source share
12 answers

The short answer is that you cannot. Bash always prints the status of foreground jobs. The monitoring flag is used only for background jobs and only for interactive shells, and not for scripts.

see notify_of_job_status () in jobs.c.

As you say, you can redirect, so a standard error points to / dev / null, but then you skip any other error messages. You can make this temporary by doing a redirection in the subshell that runs the script. This leaves the source environment separate.

 (script 2> /dev/null) 

which will lose all error messages, but only from this script, and not from anything else in this shell.

You can save and restore the standard error by redirecting a new filedescriptor to indicate 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 would not recommend this - the only potential from the first is that it saves the sub-shell call, being more complex and maybe even changing the behavior of the script if the script changes the file descriptors.




EDIT:

For a more suitable answer to the question of answer Mark Edgar

+16
Sep 17 '08 at 10:07
source share

To disable a message, you must redirect stderr while composing the message . Since the kill command sends a signal and does not wait for the response of the target process, redirecting the stderr of the kill command is it really good for you. For this purpose, bash built-in wait was specially created.

Here is a very simple example that kills the very last background command. ( More on $! Here .

 kill $! wait $! 2>/dev/null 

Since both kill and wait accept multiple pids, you can also commit group kills. Here is an example that kills all background processes (current process / script, of course).

 kill $(jobs -rp) wait $(jobs -rp) 2>/dev/null 

I was brought here from bash: silently destroy the background process .

+118
Apr 19 2018-11-21T00:
source share

Inspired by MarcHs answer . I used kill -INT as it suggests with some success, but I noticed that it did not kill some processes. After testing some other signals, I see that SIGPIPE will also be killed without a message.

 kill -PIPE 

or simply

 kill -13 
+15
Jan 04 '13 at 6:35
source share

Solution: use SIGINT (works only in non-interactive shells)

Demo:

 cat > silent.sh <<"EOF" sleep 100 & kill -INT $! sleep 1 EOF sh silent.sh 

http://thread.gmane.org/gmane.comp.shells.bash.bugs/15798

+8
Jan 31 '11 at 9:55
source share

Perhaps disconnect the process from the current shell process by calling disown ?

+4
Sep 17 '08 at 9:44
source share

Is this what we are all looking for?

Not required:

 $ sleep 3 & [1] 234 <pressing enter a few times....> $ $ [1]+ Done sleep 3 $ 

Required:

 $ (set +m; sleep 3 &) <again, pressing enter several times....> $ $ $ $ $ 

As you can see, there is no shutdown message. Works for me in bash scripts as well as for killed background processes.

'set + m' disables job management (see the β€œhelp set”) for the current shell. Therefore, if you enter your command in a subshell (as is done here in brackets), you will not affect the job management settings of the current shell. The only drawback is that you need to return the pid of your background process to the current shell if you want to check if it completed or evaluated the return code.

+1
Aug 30 '12 at 13:35
source share

This also works for killall (for those who prefer this):

 killall -s SIGINT (yourprogram) 

suppresses the message ... I am running mpg123 in the background. You could just kill him quietly by sending ctrl-c (SIGINT) instead of SIGTERM (by default).

+1
Nov 04
source share

Another way to turn off job notifications is to put your team in the background in the sh -c 'cmd &' construct.

 #!/bin/bash # ... pid="`sh -c 'sleep 30 & echo ${!}' | head -1`" kill "$pid" # ... # or put several cmds in sh -c '...' construct sh -c ' sleep 30 & pid="${!}" sleep 5 kill "${pid}" ' 
+1
Mar 08 '13 at 17:49
source share

disown did exactly what I needed - exec 3> & 2 is risky for many reasons - set + bm doesn't seem to work inside the script, only on the command line

0
Jan 28 2018-11-21T00:
source share

The success with adding ' jobs 2>&1 >/dev/null ' to the script is not defined if it helps anyone else with the script, but here is an example.

  while true; do echo $RANDOM; done | while read line do echo Random is $line the last jobid is $(jobs -lp) jobs 2>&1 >/dev/null sleep 3 done 
0
May 7, '13 at 16:34
source share

Simply:

 { kill $! } 2>/dev/null 

Advantage? any signal can be used

eg:

 { kill -9 $PID } 2>/dev/null 
0
Jun 23 '13 at 5:31 on
source share

I found that putting the kill command into a function and then phoning it overwhelms the completion output

 function killCmd() { kill $1 } killCmd $somePID & 
0
Feb 10 '18 at 22:15
source share



All Articles