How to kill the process using the name of the program instead of PID?
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?
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
Solution 1:
Use killall
:
killall <the_process_name>
Solution 2:
The pid in your example, the one that keeps changing, is the process ID of your grep trying to find the PID. That means that your ps and grep is not finding the actual process that you are looking for.
Try running your script without the trailing "&", to see what it is doing. Odds are it's failing quietly on you and not actually starting at all.
You can also use the System Monitor tool from the dash to see the full list of processes with process IDs.
Solution 3:
you can use pkill to send kill signal same as kill signal
Eg: pkill firefox
pkill -9 firefox
Solution 4:
You can kill with xkill
. Just go to the "run" dialog (Alt+F2), type in xkill
and your mouse pointer will change to an "x". Point on the application that you want to kill and click, and it'll be killed. Can sometimes be much quicker than loading the System Monitor.
Solution 5:
The basic problem is with the command 'ps ax | grep something'. This always lists 'grep ... something' as one of the process, which is in fact the 'grep' started by you. use 'pgrep ' instead to get the pid
see: https://unix.stackexchange.com/questions/74185/how-can-i-prevent-grep-from-showing-up-in-ps-results