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?

  • Are you trying to execute tornadoDeploy.py in the screen session, or are you trying to have screen run the command tornadoDeploy.py prints out, because that's what backquotes do: run the quoted command immediately, collect its standard output, replace the quoted command with its output, then finally run the full command with replaced text. – DerfK Dec 26 '10 at 20:54

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.



sudo -n python /home/matt/tornadoServer/tornadoDeploy.py &

into the file /etc/rc.d/rc.local. Or whatever the equivalent is for your distribution.

Assuming your script is a well behaved daemon and that you have set up sudo to not require a password for running your script.

Read this about startup scripts and this about a python daemon library. Also, don't you have to run tornado behind a real http server?

  • Oh yes, apparently I am indeed meant to run it behind an http server. I'm new at deploying my webservers, don't I still have to run the tornadoDeploy.py (which runs the server) just not on port 80? Can I get nginx to do that for me somehow? – DizzyDoo Dec 26 '10 at 17:41
  • I don't think so unless your tornado code can handle getting called like a CGI script, which it can't since it gets input from a socket instead of stdin and has a never-ending loop. What you need is a wrapper that switches the user id and then executes your script for safety. Just a line in rc.local like "su -c tornadoID "/home/matt/tornadoServer/tornadoDeploy.py &" where tornadoID is a restricted user id under which you will run your server. – Allen Dec 26 '10 at 23:35

The Ubuntu (and Debian) way to get applications to start at boot time is to turn them into system services using update-rc.d -- see here for instructions: http://www.debuntu.org/how-to-manage-services-with-update-rc.d

Remember that anything that runs as root is a potential security vulnerability, especially if other users can modify it.


If you run it from rc.local, you also need to edit /etc/sudoers and comment this string:

Defaults requiretty

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.