Linux: Kill a Process from the Command Line Without Knowing the PID

So we have a script – myscript.sh – which is running on a linux box and we want to kill it quickly with the minimum of fuss:

root@linux# kill $(pgrep <script_name>)

Here is the explanation:

We use “pgrep” to grep the running process list and returns a process’s PID :

[root@linux]# pgrep myscript.sh
20104

And combine it with the kill command to make a one-liner:

[root@linux]# kill $(pgrep myscript.sh)
[root@linux]#

More info on pgrep and kill on the man pages:
http://linux.die.net/man/1/pgrep

http://linux.die.net/man/2/kill

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.