Ubuntu – Run Python Server at Startup

bashpythonstartupUbuntu

I've got a few Python based servers that I need to run, and would like them to start automatically when I start my Ubuntu Server box. What is the best way to execute them like this?

I was hoping I could write a Bash script and use Screen to get them running in the background, where I can check on them every now and then, but where as

echo screen -d -m python

works just fine,

echo screen -d -m `sudo python /home/matt/tornadoServer/tornadoDeploy.py`

doesn't, with no error messages. Is that something to do with the spaces? Even though I did surround it with backquotes? I also tried:

WEB="screen -d -m `sudo python /home/matt/tornadoServer/tornadoDeploy.py`"
echo $WEB

As a way of escaping the spaces, but no luck. What's Bash scripting way to do this?

And, once the Bash script works, where can I put it to make it execute on startup?

Best Answer

Its failing because sudo is prompting for a password. Since there's no TTY open, its just waiting for you to enter one--or may, in fact, be instantly failing. If you're running this as root, you don't need to launch it as sudo.

However, what you likely want to do is alter tornadoDeploy.py to daemonize itself--that is, to detach itself, so that its not running with an open session. The python-daemonize library provides easy tools to allow you to do this. This eliminates having to deal with screen, while still allowing you to daemonize the process.