Skip to content

Instantly share code, notes, and snippets.

@wannadrunk
Last active September 20, 2022 15:06
Show Gist options
  • Save wannadrunk/a2b14b86753a7295487cb437c4159c66 to your computer and use it in GitHub Desktop.
Save wannadrunk/a2b14b86753a7295487cb437c4159c66 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