A parent process should always be running with N children process.

I would like to run a script which will show/kill all children processes that don't have a parent process.

$ ps faux | grep process.pl
root 37140  0.3  0.1  61160 724   /process.pl [child]
root 22079 10.2  0.3 127332 64620 /process.pl [master]
root 22081  0.3  0.1  84752 26084   \_ /process.pl [child]

pkill might work here:

$ pkill -P 1 process.pl

This kills all processes named process.pl whose parent is PID 1, which is what happens when a process's parent dies.

  • 4
    (assuming the parent process.pl itself has not been reparented by init as well). Note that pkill process.pl would match any process whose name contains process followed by any character followed by pl. You'd need pkill -x 'process\.pl' or pkill '^process\.pl$' for processes whose name is process.pl. – Stéphane Chazelas May 23 '13 at 17:06
  • Just make sure you do not kill process number 1! :-) – mdpc May 23 '13 at 18:35
  • Thnx, I will experiment a bit to make sure that I don't kill anything important :D – bbaja42 May 23 '13 at 19:40
  • @bbaja42: Any system with pkill on it should have pgrep, too, which has much the same command interface. So, if pgrep -P 1 -x 'process\.pl' lists all of the PIDs you want killed and nothing else, you can be reasonably sure that pkill isn't going to do something unexpected. The main danger then is that your test isn't run at the same time as the actual command, since the process table will be different at execution time. – Warren Young May 23 '13 at 19:52
  • Thanks, I'm aware of the pkill. I plan to compare output of pgrep and ps faux visually, and if all OK then run the pkill command. – bbaja42 May 24 '13 at 8:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.