Ubuntu – How to test UPSTART feature of ubuntu to see the script gets restarted automatically if it gets killed

linuxpythonUbuntuupstart

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
respawn

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 –

#!/usr/bin/python
import time

while True:
    print "Hello World"
    time.sleep(5)

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.

Best Answer

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:

#!/usr/bin/python
import time
import os

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

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

Related Question