Python: Starting Tornado Apps at Boot Using Upstart
Today, I’m going to show you how to start up your Tornado apps at boot using upstart. For path names, I’m assuming some modern version of Ubuntu, such as 9.10 or higher. I’m also assuming that you have some project with an executable Python script that fires up the Tornado app. I usually have one file in all of my projects called web.py.
The important part of this file looks like this (mind the application and port variables):
if __name__ == "__main__":
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(port)
tornado.ioloop.IOLoop.instance().start()
Got that in place? Good. Next, create the file /etc/init/<project name>.conf. It’s very important that this filename ends in .conf. The contents of this file should be as follows:
# torando project
start on runlevel 2
stop on runlevel [!2]
respawn
exec /path/to/project/web.py
That’s it! You can start your Tornado app by entering the command: sudo start <project name>. If successful, you should see output similar to: <project name> start/running, process 28058. You can also stop your projects using the command sudo stop <project name>.
The obvious caveat here is that your project is now running as root. In a future blog post, I will discuss dropping privileges at project startup. :-)
UPDATE: I’m from the future and wrote that blog post on how to drop privileges in Python for Tornado apps.


The above upstart wouldn’t work for me, I was getting path issues? I had to use this:script cd /www/foxhop.net/tornado python /www/foxhop.net/tornado/engine.py end script
Your issues were probably related to using relative paths somewhere inside your app. My recommendation then is to use os.path.join(os.path.dirname(__file__),”path”,”to”,”file”) to build absolute paths to the files you need to access.