How to manually stop a Python script that runs continuously on linux

19

11

I have a Python script that is running and continuously dumping errors into a log file.

I want to edit the script and run it again, but don't know how to stop the script.

I'm currently logged on Linux through PuTTy and am doing all the coding there. So, is there a command to stop the python script in linux?

Gunnar

Posted 2012-07-09T13:15:55.293

Reputation: 311

Answers

35

You will have to find the process id (pid). one command to do this would be

$> ps -ef

to limit results to python processes you can grep the result

$> ps -ef | grep python

which will give results like :

user      2430     1  0 Jul03 ?        00:00:01 /usr/bin/python -tt /usr/sbin/yum-updatesd

the second column is the pid. then use the kill command as such :

$> kill -9 2430 (i.e. the pid returned)

olly_uk

Posted 2012-07-09T13:15:55.293

Reputation: 476

That's exactly what i was looking for. Thank you – None – 2012-07-09T13:22:45.323

7

Try this simple line, It will terminate all script.py:

pkill -9 -f script.py

trex

Posted 2012-07-09T13:15:55.293

Reputation: 225

1This is much better than manually searching for the PID if the script name is unique. – FlippingBinary – 2019-09-08T19:36:53.140

7

Find the process id (PID) of the script and issue a kill -9 PID to kill the process unless it's running as your forground process at the terminal in which case you can Contrl-C to kill it.

Find the PID with this command:

ps -elf | grep python

It lists all the python processes, pick out the right one and note its PID. Then

kill -9 <whatever_the_PID_is>

will kill the process. You may get a message about having terminated a process at this stage.

Alternatively, you can use the top command to find the python process. Simply enter k (for kill) and the top program will prompt you for the PID of the process to kill. Sometimes it's difficult to see all processes you are interested in with top since they may scroll off the screen, I think the ps approach is easier/better.

Levon

Posted 2012-07-09T13:15:55.293

Reputation: 740

4Well, before using -9, I would kill it "softly" first. Only if it doesn't react, I would use -9. – glglgl – 2012-07-09T13:30:31.027

Not familiar with "soft killing" term .. – Levon – 2012-07-09T14:49:14.043

4

If the program is the current process in your shell, typing Ctrl-C will stop the Python program.

Ned Batchelder

Posted 2012-07-09T13:15:55.293

Reputation: 1 256

Great Solution, needed this for testing. – Kuzon – 2017-03-13T15:02:07.810

1

In a perfect world, you'd read the documentation for the script and see which signal(s) should be used to tell it to end. In real life, you probably want to send it the TERM signal, first, maybe using a KILL signal if it ignores the TERM. So, what you do is find the Process ID, using the ps command (as someone already described). Then, you can run kill -TERM <pid>. Some programs will clean up things, like files they might have open, when they get a signal like that, so it's nicer to start with something like that. If that fails, then there's not much left to do except the big hammer: kill -KILL <pid>. (you can use the numeric values, e.g. -KILL = -9, and they'll probably never change, but in a theoretical sense it might be safer to use the names)

jrl

Posted 2012-07-09T13:15:55.293

Reputation: 11

0

If you know the name of the script you could reduce all the work to a single command:

ps -ef | grep "script_name" | awk '{print $2}' | xargs sudo kill

If you want to make sure that is a python script:

ps -ef | grep "python script_name" | awk '{print $2}' | xargs sudo kill

If you wanted to kill all the python scripts:

ps -ef | grep "python" | awk '{print $2}' | xargs sudo kill

I suppose you get the idea ;)

Reminder: you need to quote "" the script name as is in the examples.

Jesus

Posted 2012-07-09T13:15:55.293

Reputation: 101