HACKER Q&A
📣 News-Dog

one-liner 'Kill Process by Name' – Bash Scriptable


I play (an old) 'Mines' game from 'SGT Puzzle Launcher' (https://launchpad.net/sgt-launcher)

Often on exit it fails to exit all process, I have written a bash cleanup script which I launch from a launcher in my panel, I add;

kill -15 $(ps -ef |grep sgt-mines |head -n1 |awk '{print $2}')

kill -15 $(ps -ef |grep sgt-launcher |head -n1 |awk '{print $2}')

to my cleanup script, which also cleans up firefox, I added;

kill -15 $(ps -ef |grep firefox |head -n1 |awk '{print $2}')

Which terminates the main firefox process, and all spawned child procs.

If the process is not running, it echoes; 'bash: kill: (pid) - No such process' because the parsed 'pid' is for the just exited grep.

Is there a better way?


  👤 News-Dog Accepted Answer ✓
Linux man pages;

pkill(1) : https://linux.die.net/man/1/pkill

killall(1) : https://linux.die.net/man/1/killall

So,

~$ pkill -f -15 sgt-mines

~$ pkill -f -15 sgt-launcher

~$ pkill -f -15 firefox


👤 junon
Alternatively:

    ps aux | grep processname | awk '{print $2}' | xargs kill
but pkill is the better option.

👤 bandie91
see killall(1) and pkill(1)

👤 SignalsFromBob
Use pkill.