Skip to content

Instantly share code, notes, and snippets.

@dev-sareno
Last active August 16, 2020 04:33
Show Gist options
  • Save dev-sareno/7f4a57b021a505030930404678da150a to your computer and use it in GitHub Desktop.
Save dev-sareno/7f4a57b021a505030930404678da150a to your computer and use it in GitHub Desktop.

Create a Linux Service Using systemd

Create a systemd Service

/etc/systemd/system/djangoexample-boot.service

[Unit]
Description=Service for django.example.com
Before=nginx.service
StartLimitIntervalSec=0

[Service]
Type=simple
User=root
KillMode=process
Restart=on-failure
ExecStart=/bin/bash -c "echo 'script started' \
        && echo "Activating venv.." \
        && source /opt/django.example.com/venv/bin/activate \
        && python -V \
        && uwsgi --chdir=/opt/django.example.com/src \
            --module=djangoexample.wsgi:application \
            --env DJANGO_SETTINGS_MODULE=djangoexample.settings \
            --master \
            --pidfile=/tmp/django.example.com-master.pid \
            --http=localhost:8000 \
            --processes=5 \
            --harakiri=20 \
            --max-requests=5000 \
            --vacuum \
            --daemonize=/var/log/uwsgi/django.example.com.log \
        && echo 'Deactivating venv..' \
        && deactivate \
        && echo 'Terminating service..' \
        && systemctl stop djangoexample-boot.service"

[Install]
WantedBy=multi-user.target

Start Service

$ systemctl start djangoexample-boot.service

Enable Service to Auto-Run on System Boot

$ systemctl enable djangoexample-boot.service

References

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