Skip to content

Instantly share code, notes, and snippets.

@mariocesar
Last active May 6, 2019 13:17
Show Gist options
  • Save mariocesar/48495532580ececa1547abbdf8bed176 to your computer and use it in GitHub Desktop.
Save mariocesar/48495532580ececa1547abbdf8bed176 to your computer and use it in GitHub Desktop.
Notes about Systemd for webapplications like Django, Flask, Aiohttp

To create the service file.

sudo vim /etc/systemd/system/webapp.service
sudo systemctl daemon-reload
sudo systemctl enable webapp.service
sudo systemctl start webapp
sudo systemctl restart webapp

If your webapp creates a unix socket, you could fix permissions by using a .path unit observer.

sudo vim /etc/systemd/system/webapp-watch.service
sudo vim /etc/systemd/system/webapp-watch.path
sudo systemctl daemon-reload
sudo systemctl enable webapp-watch.service
sudo systemctl enable webapp-watch.path
sudo systemctl start webapp-watch

A timer that will run an update daily

sudo vim /etc/systemd/system/webapp-update.service
sudo vim /etc/systemd/system/webapp-update.timer
sudo systemctl daemon-reload
sudo systemctl enable webapp-update.service
sudo systemctl enable webapp-update.timer
sudo systemctl start webapp-update
#!/usr/bin/env bash
set -e
ROOT="$(cd $(dirname "$0") && cd .. && pwd)"
VENV=${ROOT}/.venv
cd ${ROOT}
# Aiottp
${VENV}/bin/python -OO src/manage.py --no-debug runserver --path=/tmp/webapp.socket
# TODO: Flask
# TODO: Django
[Unit]
Description = Run updates
[Service]
Type = oneshot
ExecStart = /home/app/webapp/bin/update.sh
[Install]
WantedBy = multi-user.target
[Unit]
Description=Run updates daily
[Timer]
OnCalendar=daily
Persistent=true
[Install]
WantedBy=timers.target
[Unit]
Description = Watch Socket permissions
[Path]
PathChanged = /tmp/webapp.socket
[Install]
WantedBy = multi-user.target
[Unit]
Description = Fix Socket permissions
[Service]
Type = oneshot
ExecStart = /bin/chmod 777 /tmp/webapp.socket
[Install]
WantedBy = multi-user.target
[Unit]
Description=Web application
Before=nginx.service
[Service]
Type=simple
User=app
Group=app
WorkingDirectory=/home/app/webapp
ExecStart=/home/app/webapp/bin/runserver.sh
[Install]
WantedBy=multi-user.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment