I am trying to get a simple python code that I have to run in the background at startup, and stay running.

The best option that I have found so far is to use Upstart. I wrote the following .conf file in the /etc/init/ folder and the service comes up with the initctl list command however when I start the service and then check the list again it is not running. The .conf is below.

If somebody could help me that would be much appreciated

start on runlevel [2345]
stop on runlevel [016]
    exec python /some/directory/python_code.py    
end script
  • Have you checked that your python code is running correctly? It could be crashing or just exiting normally. – superdesk Jul 22 '14 at 11:44
  • Does your python daemon fork to the background? If so, you will have to use either expect fork or expect daemon in the upstart config. See the Upstart cookbook: upstart.ubuntu.com/cookbook/#how-to-establish-fork-count – CameronNemo Jul 22 '14 at 15:39
  • @superdesk my python code runs fine when I run it by entering 'python python_code.py'. However, the python code itself is an infinite while loop. Would that cause any issues with startup? – Caleb Jul 22 '14 at 17:46
  • Do you actually need to run it on start up, or would on log in do? If so, it could be done really simply. – Jacob Vlijm Jul 23 '14 at 21:17
  • @Caleb When you run it from the command line, does it take control of the console so that you can not run any more commands, or does it go to the background so that the terminal is still available? – CameronNemo Jul 23 '14 at 22:42

In my case I was able to do what I wanted using crontab.

I didn't necessarily need to run the code at startup. I just wanted to know that it would run while the computer is on. Using crontab allows me to run the python code every 15 minutes whenever the computer is on which works great.

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.