Question

I am currently writing a script that must, among other things, kill some processes.

I used the kill method and it works perfectly, except that, when I launch my script, some unwanted messages are print in the console saying for each of the processes that they have been "terminated".

I don't know how to get rid of these messages, so I would be grateful to anyone who can help me.

I would like these messages not to print inside the console, for they can be misleading. If I understand it correctly, those are error messages printed in the stderr stream.

Is there a way I can prevent these logs to print in the console ?

I have found on this site three issues that seem identical to mine, and proposed three differents solutions:

kill -9 $pid &> /dev/null
exec 3>&2
exec 2> /dev/null
kill $pid &> /dev/null
exec 2>&3
exec 3>&-

kill $pid

wait $pid 2>/dev/null

But sadly none of these solutions change the behavior on my script (I always have "Terminated" messages). Does anyone have any other ideas? I am relatively new to Linux and does not master the redirection of outputs yet, so maybe I am forgetting something really obvious?

A huge thanks to anybody who will try to help!

Was it helpful?

Solution

Use a subshell to start the processes and divert the output of the subshell:

pid=$(sleep 10 >/dev/null 2>&1 & echo $!)
kill $pid

The problem is that the starting shell will monitor and notice the termination of its child and report on it.

You also can be more drastic and switch off job control (which in a script might be just what you want):

set +m
sleep 3 & pid=$!
kill $pid
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top