Skip to content

Instantly share code, notes, and snippets.

@toast38coza
Last active March 3, 2022 11:30
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save toast38coza/734c89775be9ddd69351 to your computer and use it in GitHub Desktop.
Save toast38coza/734c89775be9ddd69351 to your computer and use it in GitHub Desktop.
Simple systemd unit for running a django app
# start / stop / restart / status
systemctl start test
systemctl stop test
systemctl restart test
systemctl status test
# logs use journalctl:
# tail the logs for unit `django`
journalctl -f -u django
[Unit]
Description=Unit for starting a basic Django app
[Service]
Restart=on-failure
WorkingDirectory=/var/www/hello
ExecStart=/srv/env/bin/gunicorn hello.wsgi -b 0.0.0.0:8000
[Install]
WantedBy=multi-user.target
@clbarnes
Copy link

clbarnes commented Feb 3, 2021

How would you express a dependency on a database? I'm struggling with the combination of factors After, Requres, BindsTo, PartOf etc....

@mikbuch
Copy link

mikbuch commented Feb 12, 2022

@clbarnes, it seems that you can use both: After and Requires (see this answer; see also this question). I will be trying to achieve something similar in terms of the order of starting the services with systemd. I will let you know about the outcome.

Normally, I'd use a docker-compose (and dockerization in general) to achieve services dependency (which I highly recommend, if you don't have any restrictions it), but currently I am struggling with an embedded system (Raspbian), and the docker doesn't seem to work in the setup I have, thus I returned to the good ol' systemd.

@mikbuch
Copy link

mikbuch commented Feb 12, 2022

Generally about the gist, for convenience I'd mention where the systemd files are stored:

vim /etc/systemd/system/django.service

maybe in the comment at the beginning of the file?

# vim /etc/systemd/system/django.service

[Unit]
Description=Unit for starting a basic Django app 

[Service]
Restart=on-failure
...

See also my blog post.

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