Two ways of doing it. Using supervisord
or systemd
, well probably more, but this is easy.
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/
.
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