Skip to content

Instantly share code, notes, and snippets.

@dunderrrrrr
Created February 21, 2020 13:38
Show Gist options
  • Save dunderrrrrr/24f922b48c7bb61cd72f52595fb39727 to your computer and use it in GitHub Desktop.
Save dunderrrrrr/24f922b48c7bb61cd72f52595fb39727 to your computer and use it in GitHub Desktop.
Two ways of doing it. Using `supervisord` or `systemd`, well probably more, but this is easy.

Two ways of doing it. Using supervisord or systemd, well probably more, but this is easy.

supervisord

Supervisor is a client/server system that allows its users to monitor and control a number of processes on UNIX-like operating systems.

$ sudo apt-get install supervisor

Create a config file for your daemon at /etc/supervisor/conf.d/your-service.conf.

[program:your-service]
directory=/home/user/scripts
command=/home/user/virtualenvs/your-service/bin/python /home/user/scripts/your-service.py
autostart=true
autorestart=true

Restart supervisor to load config.

$ supervisorctl update
$ supervisorctl restart your-service

You can now start and stop your service. Logs are located in /var/log/supervisor/.

systemd

The systemd software suite provides fundamental building blocks for a Linux operating system. It includes the systemd "System and Service Manager", an init system used to bootstrap user space and manage user processes.

Create a config file at /etc/systemd/system/my_daemon.service.

[Unit]
Description=My script

[Service]
Type=simple
ExecStart=/home/user/virtualenvs/your-service/bin/python /home/user/scripts/your-service.py
WorkingDirectory=/home/user/scripts/
Restart=always
RestartSec=2

[Install]
WantedBy=sysinit.target

Enable your service.

$ sudo systemctl daemon-reload
$ sudo systemctl enable my_daemon
$ sudo systemctl start my_daemon --no-block

Logs can be show running

$ sudo systemctl status my_daemon
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment