I'm writing a script that tries to kill a process. But the process might already been killed by others. How can I guard against it and prevent this kill command from terminating my process with throwing an error? I have PID
of the process
Asked
Active
Viewed 364 times
1 Answers
0
Look into using pkill instead of kill. It will return 1 if no process matched. Test exit status of pkill to determine if it actually killed anything

Jason Lewis
- 175
kill
? – Joseph R. Jun 10 '14 at 17:55-e
. – Joseph R. Jun 10 '14 at 18:25kill pid || true
. When all you have is a PID, it is impossible to avoid race conditions if the state of the PID changes between when you check it and when you try to do something with it. – jw013 Jun 10 '14 at 18:260
– Mohsen Jun 12 '14 at 03:19kill
on the PIDs and thenexit 0
s unconditionally. – ctt Jun 12 '14 at 11:27