6

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
Braiam
  • 67,791
  • 32
  • 179
  • 269
arsenal
  • 1,993

6 Answers6

5

Use killall:

killall <the_process_name>
rusty
  • 16,327
  • In this case, what it should be? killall zook.py – arsenal Dec 28 '13 at 05:53
  • @TechGeeky: yes... that should kill the script... as for my experience with killing bash scripts, it works; sometimes I do have to kill other running processes started by the script... – rusty Dec 28 '13 at 06:00
  • 1
    It doesn't work for me somehow.. It is saying no such process.. – arsenal Dec 28 '13 at 06:01
  • probably the script's already killed... did you try killing the invoked processes ? – rusty Dec 28 '13 at 06:03
  • How do I do that? This is what I am getting when I did ps ax | grep zook.py 23248 pts/6 S+ 0:00 grep --color=auto zook.py – arsenal Dec 28 '13 at 06:06
  • use cat zook.py to view the processes started by the script... check if they're still running with top; if they're running kill... if there not I guess you're alright! – rusty Dec 28 '13 at 06:23
3

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.

2

you can use pkill to send kill signal same as kill signal

Eg: pkill firefox 
pkill -9 firefox
Meow
  • 1,357
2

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.

kamil
  • 7,355
  • 7
  • 40
  • 61
1

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

Vishu
  • 11
0

Best answered here, https://askubuntu.com/a/760514/877732

If myName is not the process name or for example, is an argument to another (long) command, pkill (or pgrep) might not work as expected. So you need to use the -f option.