<p>How can you suppress the <code>Terminated</code> message that comes up after you kill a
process in a bash script?</p>

<p>I tried <code>set +bm</code>, but that doesn't work.</p>

<p>I know another solution involves calling <code>exec 2&gt; /dev/null</code>, but is that
reliable? How do I reset it back so that I can continue to see stderr?</p>

[{'answer_id': 81539, 'author': 'Matthias Kestenholz', 'author_id': 317346, 'author_profile': 'https://Stackoverflow.com/users/317346', 'pm_score': 3, 'selected': False, 'text': '<p>Maybe detach the process from the current shell process by calling <code>disown</code>?</p>\n'}, {'answer_id': 81672, 'author': 'wnoise', 'author_id': 15464, 'author_profile': 'https://Stackoverflow.com/users/15464', 'pm_score': 5, 'selected': True, 'text': '<p>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.</p>\n\n<p>see notify_of_job_status() in jobs.c.</p>\n\n<p>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.</p>\n\n<pre><code>(script 2&gt; /dev/null)\n</code></pre>\n\n<p>which will lose all error messages, but just from that script, not from anything else run in that shell.</p>\n\n<p>You can save and restore standard error, by redirecting a new filedescriptor to point there:</p>\n\n<pre><code>exec 3&gt;&amp;2          # 3 is now a copy of 2\nexec 2&gt; /dev/null  # 2 now points to /dev/null\nscript             # run script with redirected stderr\nexec 2&gt;&amp;3          # restore stderr to saved\nexec 3&gt;&amp;-          # close saved version\n</code></pre>\n\n<p>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.</p>\n\n<hr>\n\n<p><strong>EDIT:</strong></p>\n\n<p><em>For more appropriate answer check answer given by <a href="https://stackoverflow.com/users/129332/mark-edgar">Mark Edgar</a></em></p>\n'}, {'answer_id': 4833055, 'author': 'clemep', 'author_id': 594384, 'author_profile': 'https://Stackoverflow.com/users/594384', 'pm_score': 1, 'selected': False, 'text': "<p>disown did exactly the right thing for me -- the exec 3>&amp;2 is risky for a lot of reasons -- set +bm didn't seem to work inside a script, only at the command prompt</p>\n"}, {'answer_id': 4849503, 'author': 'MarcH', 'author_id': 317623, 'author_profile': 'https://Stackoverflow.com/users/317623', 'pm_score': 4, 'selected': False, 'text': '<p>Solution: use SIGINT (works only in non-interactive shells)</p>\n\n<p>Demo:</p>\n\n<pre><code>cat &gt; silent.sh &lt;&lt;"EOF"\nsleep 100 &amp;\nkill -INT $!\nsleep 1\nEOF\n\nsh silent.sh\n</code></pre>\n\n<p><a href="http://thread.gmane.org/gmane.comp.shells.bash.bugs/15798" rel="noreferrer">http://thread.gmane.org/gmane.comp.shells.bash.bugs/15798</a></p>\n'}, {'answer_id': 5722874, 'author': 'Mark Edgar', 'author_id': 129332, 'author_profile': 'https://Stackoverflow.com/users/129332', 'pm_score': 7, 'selected': False, 'text': '<p>In order to silence the message, you must be redirecting <code>stderr</code> <strong>at the time the message is generated</strong>. Because the <a href="http://ss64.com/bash/kill.html" rel="noreferrer"><code>kill</code></a> command sends a signal and doesn\'t wait for the target process to respond, redirecting <code>stderr</code> of the <code>kill</code> command does you no good. The bash builtin <a href="http://ss64.com/bash/wait.html" rel="noreferrer"><code>wait</code></a> was made specifically for this purpose.</p>\n\n<p>Here is very simple example that kills the most recent background command. (<a href="https://stackoverflow.com/a/5163260/117471">Learn more about $! here.</a>)</p>\n\n<pre><code>kill $!\nwait $! 2&gt;/dev/null\n</code></pre>\n\n<p>Because both <code>kill</code> and <code>wait</code> 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).</p>\n\n<pre><code>kill $(jobs -rp)\nwait $(jobs -rp) 2&gt;/dev/null\n</code></pre>\n\n<p>I was led here from <a href="https://stackoverflow.com/questions/5719030/bash-silently-kill-background-function-process/5722850">bash: silently kill background function process</a>.</p>\n'}, {'answer_id': 12198152, 'author': 'Ralph', 'author_id': 1636189, 'author_profile': 'https://Stackoverflow.com/users/1636189', 'pm_score': 2, 'selected': False, 'text': "<p>Is this what we are all looking for?</p>\n\n<p>Not wanted: </p>\n\n<pre><code>$ sleep 3 &amp;\n[1] 234\n&lt;pressing enter a few times....&gt;\n$\n$\n[1]+  Done                    sleep 3\n$\n</code></pre>\n\n<p>Wanted:</p>\n\n<pre><code>$ (set +m; sleep 3 &amp;)\n&lt;again, pressing enter several times....&gt;\n$\n$\n$\n$\n$\n</code></pre>\n\n<p>As you can see, no job end message. Works for me in bash scripts as well, also for killed background processes.</p>\n\n<p>'set +m' disables job control (see 'help set') for the current shell. So if you enter your command in a subshell (as done here in brackets) you will not influence the job control settings of the current shell. Only disadvantage is that you need to get the pid of your background process back to the current shell if you want to check whether it has terminated, or evaluate the return code.</p>\n"}, {'answer_id': 13223242, 'author': 'Coder of Salvation', 'author_id': 1659796, 'author_profile': 'https://Stackoverflow.com/users/1659796', 'pm_score': 2, 'selected': False, 'text': '<p>This also works for killall (for those who prefer it):</p>\n\n<pre class="lang-sh prettyprint-override"><code>killall -s SIGINT (yourprogram) \n</code></pre>\n\n<p>suppresses the message... I was running mpg123 in background mode.\nIt could only silently be killed by sending a ctrl-c (SIGINT) instead of a SIGTERM (default).</p>\n'}, {'answer_id': 15300185, 'author': 'phily', 'author_id': 2149422, 'author_profile': 'https://Stackoverflow.com/users/2149422', 'pm_score': 0, 'selected': False, 'text': '<p>Another way to disable job notifications is to place your command to be backgrounded in a <code>sh -c \'cmd &amp;\'</code> construct.</p>\n\n<pre><code>#!/bin/bash\n# ...\npid="`sh -c \'sleep 30 &amp; echo ${!}\' | head -1`"\nkill "$pid"\n# ...\n\n# or put several cmds in sh -c \'...\' construct\nsh -c \'\nsleep 30 &amp;\npid="${!}"\nsleep 5 \nkill "${pid}"\n\'\n</code></pre>\n'}, {'answer_id': 16424178, 'author': 'J-o-h-n-', 'author_id': 2359158, 'author_profile': 'https://Stackoverflow.com/users/2359158', 'pm_score': 0, 'selected': False, 'text': "<p>Had success with adding '<code>jobs 2&gt;&amp;1 &gt;/dev/null</code>' to the script, not certain if it will help anyone else's script, but here is a sample.</p>\n\n<pre><code>    while true; do echo $RANDOM; done | while read line\n    do\n    echo Random is $line the last jobid is $(jobs -lp)\n    jobs 2&gt;&amp;1 &gt;/dev/null\n    sleep 3\n    done\n</code></pre>\n"}, {'answer_id': 17257986, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': -1, 'selected': False, 'text': '<p>Simple:</p>\n\n<pre class="lang-sh prettyprint-override"><code>{ kill $! } 2&gt;/dev/null\n</code></pre>\n\n<p>Advantage? can use any signal</p>\n\n<p>ex:</p>\n\n<pre class="lang-sh prettyprint-override"><code>{ kill -9 $PID } 2&gt;/dev/null\n</code></pre>\n'}, {'answer_id': 48726125, 'author': 'Al Joslin', 'author_id': 2912739, 'author_profile': 'https://Stackoverflow.com/users/2912739', 'pm_score': -1, 'selected': False, 'text': '<p>I found that putting the kill command in a function and then backgrounding the function suppresses the termination output</p>\n\n<pre><code>function killCmd() {\n    kill $1\n}\n\nkillCmd $somePID &amp;\n</code></pre>\n'}, {'answer_id': 66355103, 'author': 'James Z.M. Gao', 'author_id': 7704140, 'author_profile': 'https://Stackoverflow.com/users/7704140', 'pm_score': 2, 'selected': False, 'text': '<p>The <code>Terminated</code> is logged by the default signal handler of bash 3.x and 4.x. Just trap the TERM signal at the very first of child process:</p>\n<pre class="lang-sh prettyprint-override"><code>#!/bin/sh\n\n## assume script name is test.sh\n\nfoo() {\n  trap \'exit 0\' TERM ## here is the key\n  while true; do sleep 1; done\n}\n\necho before child\nps aux | grep \'test\\.s[h]\\|slee[p]\'\n\nfoo &amp;\npid=$!\n\nsleep 1 # wait trap is done\n\necho before kill\nps aux | grep \'test\\.s[h]\\|slee[p]\'\n\nkill $pid ## no need to redirect stdin/stderr\n\nsleep 1 # wait kill is done\n\necho after kill\nps aux | grep \'test\\.s[h]\\|slee[p]\'\n\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/81520', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/14437/']