Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

migrated from Jul 9 '12 at 19:54

This question came from our site for professional and enthusiast programmers.

up vote 15 down vote accepted

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)
share|improve this answer
That's exactly what i was looking for. Thank you – Gunnar Jul 9 '12 at 13:22

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.

share|improve this answer
That's exactly it. Thank you – Gunnar Jul 9 '12 at 13:22
Well, before using -9, I would kill it "softly" first. Only if it doesn't react, I would use -9. – glglgl Jul 9 '12 at 13:30
Not familiar with "soft killing" term .. – Levon Jul 9 '12 at 14:49

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

share|improve this answer

Try this simple line, It will terminate all

pkill -9 -f
share|improve this answer

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)

share|improve this answer

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.

share|improve this answer

You must log in to answer this question.

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