Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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 stackoverflow.com Jul 9 '12 at 19:54

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

5 Answers 5

up vote 7 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
2  
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

If you know the name of the process you could just say (stop "process name"). And to start it back up (start "process name")

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.