Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save marcanuy/5ae0e0ef5976aa4a10a7 to your computer and use it in GitHub Desktop.
Save marcanuy/5ae0e0ef5976aa4a10a7 to your computer and use it in GitHub Desktop.
Using Systemd to Make Sure Gunicorn Starts on Boot. As Ubuntu has switched to systemd as its service framework starting in 15.04 for all flavors, this is a migration of the Gunicorn upstart job that appears in "Test Driven Development with Django" (http://chimera.labs.oreilly.com/books/1234000000754/ch08.html#_using_upstart_to_make_sure_gunicorn…
# Gunicorn Site systemd service file
[Unit]
Description=Gunicorn server for SITENAME-staging.example.com
After=network.target
After=syslog.target
Environment=sitedir=/Development/sites/SITENAME-staging.example.com
ExecStart=$(sitedir)/virtualenv/bin/gunicorn --chdir $(sitedir)/source workouts.wsgi:application --bind unix:/tmp/SITENAME-staging.example.com.socket
Restart=on-failure
RuntimeDirectory=gunicorn-stagingd
RuntimeDirectoryMode=755
#sudo systemctl start gunicorn-SITENAME-staging.example.com.service
@Camsbury
Copy link

Camsbury commented Sep 3, 2016

Thank you so much. I just spent like 2 hours trying to figure this out. Hopefully it will work in Ubuntu 16.04...

@mkmoisen
Copy link

The Restart=on-failure option doesn't work for me. I added it, then reloaded the config via systemctl daemon-reload, restarting my application and then killed its process, but it didn't restart on failure. Any idea?

@vioan
Copy link

vioan commented Sep 27, 2016

@mkmoisen, if you did "kill -9 pid" it should restart, but if you did "killall gunicorn" will not restart because killall is sending a SIGTERM signal.

If set to on-failure, the service will be restarted when the process exits with a non-zero exit code, is terminated by a signal (including on core dump, but excluding the SIGHUP, SIGINT, SIGTERM or SIGPIPE signals). Because these four mentioned signals means clean exit.

@marcanuy
Copy link
Author

@AlirezaAzhdari
Copy link

Thank you. that --chdir saved me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment