How to let 'kill' ignore processes that are not alive

3

How to tell the kill command to ignore processes if that process is not alive?

For example: 3453 is an alive process but 44534 is not.

kill -9 3453 44534

yael

Posted 2010-06-18T12:09:02.227

Reputation:

1Also, what you're asking is not quite clear. Could you rephrase or elaborate to say exactly what you want to happen? – lc. – 2010-06-18T12:13:33.183

Answers

3

for pid in 3453 44534
do
  kill -9 "$pid" > /dev/null 2> /dev/null || :
done

Ignacio Vazquez-Abrams

Posted 2010-06-18T12:09:02.227

Reputation: 100 516

2

kill -9 3453 || kill -9 44535

luke

Posted 2010-06-18T12:09:02.227

Reputation:

And what if the situation were reversed? Or if both are alive? – Ignacio Vazquez-Abrams – 2010-06-23T20:23:00.783

0

taskid=12345

if ps ax | grep -v grep | grep $taskid > /dev/null; then
  kill -9 $taskid
  # Task killed
fi

JRobert

Posted 2010-06-18T12:09:02.227

Reputation: 6 128

0

Most people are not aware that kill is a built-in command for many shells and I am assuming that you are using bash and that you would like to suppress the "no such process" messages that may be generated. The executable located in /bin/kill on one of my machines generates no such messages.

$ /bin/kill -9 3453 44534

gestep

Posted 2010-06-18T12:09:02.227

Reputation: 81

1and it also generates error... – OZ_ – 2015-11-08T11:52:30.777