Skip to content

Instantly share code, notes, and snippets.

@TheLustriVA
Forked from wannadrunk/python-as-service.md
Created September 20, 2022 15:06
Show Gist options
  • Save TheLustriVA/b59c5bd445497ac0f56e55648a4f21fa to your computer and use it in GitHub Desktop.
Save TheLustriVA/b59c5bd445497ac0f56e55648a4f21fa to your computer and use it in GitHub Desktop.
How to run Python script as systemd service.
  1. in Python script, make sure include #!/path/to/venv/python in the first line
  2. add a systemd service $ sudo systemctl edit --force --full name.service. In the editor complete at least these statments,
[Unit]
Description=Python Service
Wants=network.target
After=network.target

[Service]
ExecStartPre=/bin/sleep 10
ExecStart=/path/to/bot.py
Restart=always

[Install]
WantedBy=multi-user.target
  1. enable the systemd service, sudo systemctl enable name.service and start the service sudo systemctl start name.service
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment