chainsawriot

Home | About | Archive

Unix utilities #4: Kill process by name

Posted on Nov 17, 2013 by Chung-hong Chan

I need to kill some specific jobs by the process name (e.g. qqweibo) regularly and an ex-coworker of mine suggested this:

kill `ps faux | grep qqweibo | cut -d" " -f6-7`

This method is not robust. The problem arises from the cut command, it cannot extract the PID from the "ps faux" in some cases e.g. when the PID is less than <1000, it will slip through.

A more robust way is to

kill `ps -ef | grep qqweibo | grep -v grep | awk '{print $2}'`

This method is slower (awk is slower than cut) but I don't mind that <0.5s of delay.


Powered by Jekyll and profdr theme