Saturday 13 April 2019

How to run gunicorn script using systemd in Debian

Since upstart is deprecated, it is not available in Debian 9, so I had to use systemd which turned out to be quite easy:

nano  /etc/systemd/system/gunicorn.service

Create the file:

[Unit]
Description=Gunicorn Daemon
#After=network.target
StartLimitIntervalSec=0
[Service]
Type=simple
User= appuser
Group=www-data
ExecStart=/path/to/project/gunicorn.sh
#Restart=always
#RestartSec=1
#Restart=on-failure
# Configures the time to wait before service is stopped forcefully.
TimeoutStopSec=300
[Install]
WantedBy=multi-user.target
You don't need to restart the server to enable the service:

systemctl enable gunicorn
Start it:
systemctl start gunicorn

Watch it:
systemctl status gunicorn
If you made changes to gunicorn.service you need to run:

systemctl daemon-reload

before restarting the daemon with

systemctl restart gunicorn