Skip to content

Instantly share code, notes, and snippets.

@bzamecnik
Last active November 16, 2023 08:58
Show Gist options
  • Save bzamecnik/76ef9db8edf64922563654ca1b05075f to your computer and use it in GitHub Desktop.
Save bzamecnik/76ef9db8edf64922563654ca1b05075f to your computer and use it in GitHub Desktop.
Anaconda Python script running as systemd service

Anaconda Python script running as systemd service

This way a Python daemon can be installed on Rasbian, Ubuntu or similar systems using systemd.

Installing:

sudo cp hello.service /lib/systemd/system/hello.service
sudo systemctl daemon-reload
sudo systemctl enable hello.service

Running:

sudo systemctl start hello.service

Other commands:

sudo systemctl status hello.service
sudo systemctl restart hello.service
sudo systemctl stop hello.service
# some dummy service
import time
while True:
print('Hello, world!')
time.sleep(1000)
[Unit]
Description=Hello, world! service
[Service]
Type=simple
WorkingDirectory=/some/path
ExecStart=/some/path/to/start.sh
[Install]
WantedBy=multi-user.target
Alias=hello.service
#!/bin/sh
# - the script is ran via anaconda python
# - output is not buffered (-u)
# - stdout & stderr are stored to a log file
# - we're executing the working directory specified in the systemd service file
$HOME/miniconda3/bin/python3 -u hello.py 2>&1 >> hello.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment