I am running my Python script using upstart feature of Ubuntu so that if for whatever reason my Python script dies or gets killed, it can be restarted automatically.

So I decided to use UPSTART feature of Ubuntu to restart the Python script automatically.

After creating the testing.conf file like this in /etc/init/testing.conf -

chdir /tekooz
exec python testing.py

I ran below sudo command to start it and I can see that process running using ps ax and my python script is also running fine.

root@bx13:/tekooz# sudo start testing
testing start/running, process 27794

This is my below python script -

import time

while True:
    print "Hello World"

But how do I test it to see if my Python script gets killed, then it is getting restarted automatically? I cannot kill the PID as the PID keeps on changing if I do ps ax on my testing.py.

Can anyone tell me how to test this scenario? I am just trying to make sure my script can be restarted automatically if it gets killed or dies.


2 Answers 2


Just run:

sudo status testing

that gives you the status of the running upstart service.

And with tail -f /var/log/syslog you can see if it is respawning.

The "Hello World" goes is I think going nowhere.

I recommend testing with:

import time
import os

with open('/var/tmp/testing.log', 'a') as fp:
        while True:
            print >> fp, "Hello World", os.getpid()
    except Exception as e:
        print >> fp, 'exception', e

and run tail -f /var/tmp/testing.log in an other window.

  • Does it get respawning every few seconds? Or it will see whether it is not running or got killed? In general it will keep on launching testing.py every few seconds? or it will launch it once and see whether it is running or not.. If it is not running, then it will restart it again?
    – arsenal
    Jan 6, 2014 at 10:52
  • My mistake, my testing.py did not have while True:
    – Zelda
    Jan 6, 2014 at 10:53
  • @Webby why do you not write the Hello World to a file for debugging, with the process number?
    – Zelda
    Jan 6, 2014 at 10:54
  • Sorry. In my testing.py I have your python script example that you mentioned to me in your previous answer. I thought I have mine, in your example yes the PID is getting changed automatically. But if I use my HelloWorld while True example, the PID is not changing so I am assuming, it will check whether my Python script is running or not. If it is not running, then only it will launch it again? Otherwise it won't do anything..
    – arsenal
    Jan 6, 2014 at 10:59
  • @Webby Zelda's script has no while true;, it is simply printing the time once and exiting. Since your upstart job has a respawn command, it will be relaunching the script constantly and, therefore, changing its PID.
    – terdon
    Jan 6, 2014 at 11:00

First of all, why is the PID changing? That makes no sense. If you kill the process and see the PID change, that means it was restarted as expected. If you are not killing the script but the PID changes regardless, then your script is getting killed by something and is respawning.

Anyway, you can kill by PID, by name (sudo pkill testing.py) or by killing all running python processes (drastic, but assuming that's the only one running you should be fine): sudo killall python.

  • Thank You terdon. From your comment I figured out the PID should not change and then I went back and verified my Python script and I did a mistake in my script as I thought I have my while True script.
    – arsenal
    Jan 6, 2014 at 11:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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