Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 4 down vote accepted

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.

share|improve this answer
    
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? –  Webby Jan 6 at 10:52
    
My mistake, my testing.py did not have while True: –  Zelda Jan 6 at 10:53
    
@Webby why do you not write the Hello World to a file for debugging, with the process number? –  Zelda Jan 6 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.. –  Webby Jan 6 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 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.

share|improve this answer
    
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. –  Webby Jan 6 at 11:04

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.