Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A slightly Raspberry Pi specific Systemd service unit example.
service_name="sample.service"
service_desc="My Sample Service"
service_cmd="/usr/bin/python /home/pi/sample.py"
cat > /lib/systemd/system/$service_name <<EOF
[Unit]
Description=$service_desc
After=multi-user.target
[Service]
# To cd first:
#WorkingDirectory=/usr/local
# Service Types: https://wiki.archlinux.org/index.php/systemd#Service_types
Type=idle
ExecStart=$service_cmd
# Or, to save stdout and stderr to a log file:
#ExecStart=$service_cmd > /home/pi/$service_name.log 2>&1
[Install]
WantedBy=multi-user.target
EOF
sudo chmod 644 /lib/systemd/system/$service_name
sudo systemctl daemon-reload
sudo systemctl enable $service_name
sudo reboot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.