33

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?

0

7 Answers 7

51

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)
0
20

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

pkill -9 -f script.py
1
  • 3
    This is much better than manually searching for the PID if the script name is unique. Sep 8, 2019 at 19:36
8

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.

2
  • 4
    Well, before using -9, I would kill it "softly" first. Only if it doesn't react, I would use -9.
    – glglgl
    Jul 9, 2012 at 13:30
  • Not familiar with "soft killing" term ..
    – Levon
    Jul 9, 2012 at 14:49
5

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

2
  • Great Solution, needed this for testing.
    – Kuzon
    Mar 13, 2017 at 15:02
  • If you are running a process in a loop it will only kill that process rather than the python script. For example, I have a python script to read URLs from a file, and for each URL it will run youtube-dl to download the video. If I press Ctrl + C it will only kill the youtube-dl process. Then it will move on to the next item.
    – Hind-D
    Sep 30, 2021 at 5:20
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)

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.

-1

When your script running continuously Press CTRL+Z to stop the execution.

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .