vellezz.info

python w virtualenv i systemd

Jan
22

Sposób rozwiązania problemu uruchomienia skryptu python jako usługi w systemie opartym na systemd (w tym przypadku Ubuntu 16.04LTS).

Tworzymy plik konfiguracyjny usługi w /etc/systemd/system/<python_service_name>.service

touch /etc/systemd/system/<python_service_name>.service

Edytujemy

vi /etc/systemd/system/<python_service_name>.service

Zawartość pliku usługi :

[Unit]
Description=Intranet Zimbra Websocket
After=network.target

[Service]
Type=simple
User=<user>
WorkingDirectory=<working_dir>
Environment=VIRTUAL_ENV="<virualenv_bin_dir"
Environment=HOMEDIR="<python_script_home_dir>"
Environment=PATH="$VIRTUAL_ENV/bin:/usr/bin:/bin:$PATH"
Restart=always
ExecStart=<python_script_home_dir>/<python_script_name>.py

[Install]
WantedBy=multi-user.target

Tworzymy plik /etc/init/<python.conf

touch /etc/init/<python_service_name>.conf

Edytujemy:

vi /etc/init/<python_service_name>.conf

Zawartość pliku konfiguracyjnego:

description "<description>"
author "<author>"

start on runlevel [2345]
stop on runlevel [!2345]

respawn.
respawn limit 20 5

setuid
setgid

exec <virtual_env_bin_dir>/python <python_script_name>.py


Uruchamiamy usługę

service <service_name> start

Autostart:

systemctl enable <service_name>.service

Życzę powodzenia 🙂

PS. w miejscach oznaczonych <> należy podać odpowiednie wartości

 

Leave a Reply