Linux – How to kill the process using the name of the program instead of PID

background-processlinuxnohuppythonunix

I started my Python program in the background using nohup as mentioned below –

nohup zook.py &

Now I am trying to kill this process so I did the ps command as mentioned below

root@phxdbx1145:/home/david/zook# ps ax | grep zook.py
16352 pts/6    S+     0:00 grep --color=auto zook.py

But somehow, everytime its PID getting changed, I don't know why. Whenever I do like this –

kill -9 16352

It always say, No Such Process.

And when I do px command again, I see that PID got changed automatically..

So I am not sure how do I kill this process?

Is there any way I can kill the process with the name somehow?

I tried with

killall zook.py

but it doesn't work for me and I get –

zook.py: no process found

Any other option? Somehow everytime, its PID getting changed.. So I cannot use kill -9 pid

UPDATE:-

This is what I am getting. I did pkill -9 zook.py and then I did ps command as mentioned below and it is shwoing zook.py constantly?

root@dbx1145:/home/david/zook# pkill -9 zook.py

root@dbx1145:/home/david/zook# ps ax | grep zook.py
23870 pts/6    S+     0:00 grep --color=auto zook.py

root@dbx1145:/home/david/zook# ps ax | grep zook.py
23872 pts/6    S+     0:00 grep --color=auto zook.py

root@dbx1145:/home/david/zook# ps ax | grep zook.py
23874 pts/6    S+     0:00 grep --color=auto zook.py

root@dbx1145:/home/david/zook# ps ax | grep zook.py
23876 pts/6    S+     0:00 grep --color=auto zook.py

Best Answer

If you look at what is to the right of that PID you may notice that it is the PID of grep looking for your search string. That is why it is different every time. By the time you have your shell back from grep, of course grep is gone.

You will find your process under a different name, if it is still running. Try ps ax | grep python instead, or better yet, pgrep python if you have it.