1

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 and everything is working fine -

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 -

start on runlevel [2345]
stop on runlevel [016]

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

Now if I kill the PID of the above process, then it get restarted automatically within few milliseconds or microseconds.

Is there any possibility of restarting my python script after 1 second if it has gone down or got killed? I don't want to restart immediately.

  • Consider putting the conf file in ~/.config/upstart/ so that you don't need sudo: you would use initctl start testing to start the script for example. (I am assuming that your conf file doesn't need root privileges.) – edwin Jan 25 '14 at 19:52
  • Ok. But my question is different than what you are suggesting. May be you are suggesting something else not related to my question. – arsenal Jan 25 '14 at 19:58
  • 1
    That's why I commented and did not "answered" your question. – edwin Jan 25 '14 at 20:14
  • @edwin: Now I am realizing it's a bad idea to run with sudo start testing. In your example you told me I can place the file in ~/.config/upstart/ folder but I am not able to find this folder? Can you provide me the full path where I can put my file? – arsenal Apr 9 '14 at 20:38
  • On Nautilus, go to your home folder and press Ctrl+H to show hidden files, you should see the .config directory. Inside .config, you need to create a folder named upstart (that is, if it doesn't exist just create it). – edwin Apr 10 '14 at 0:24
3

I don't really understand why you are using the root directory for this "test", and I don't really think that's what you want to do (you can end up breaking your system badly). Also, notice that if you are root (the # prompt) you don't need to "sudo". Please consider editing the question to reflect what you are trying to achieve.

I will repeat, for the sake of completeness:

Consider putting the conf file in ~/.config/upstart/ so that you don't need to sudo: you would use initctl start testing to start the script for example. (I am assuming that your conf file doesn't need root privileges.)

On reading the cookbook, it says that the pre-start, post-start, and post-stop stanzas are rerun on a respawned job. This means you can do something like this:

start on runlevel [2345]
stop on runlevel [016]

chdir /path/to/a/folder
respawn

exec python testing.py

post-stop script
  sleep 1
end script

This way there will be a 1 second delay before the script is respawned. (You can do the same with the pre-start stanza, this way the first invocation would have a 1 second delay, though.)

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.