Skip to content

Instantly share code, notes, and snippets.

@danawoodman
Last active February 7, 2024 00:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danawoodman/096dd0f17f7d6b7e84ba39d23079aa7c to your computer and use it in GitHub Desktop.
Save danawoodman/096dd0f17f7d6b7e84ba39d23079aa7c to your computer and use it in GitHub Desktop.
Setup a system service for the RaspberryPi

Setup a system service for the RaspberryPi

If you want to run your own app on a Debian system, like a RaspberryPi, you'll need to build the binary for the given device then setup systemd to run the binary.

Given you have an app called myapp, first create a service file to run your app, say at myapp.service on your local system:

[Unit]
Description=Some description here...
After=network.target

[Service]
Restart=always
RestartSec=3
# setup any env vars the process needs:
Environment=PORT=80 SOMETHING_ELSE=true
# path to binary to run:
ExecStart=/usr/local/sbin/myapp


[Install]
WantedBy=multi-user.target

Then, in the terminal, copy over the binary and your service file and then configure them to run.

Build your app (e.g. with golang, "go build ."), then make it executable (if needed):

sudo chmod +x myapp

Copy to device:

scp myapp myapp.service user@host.local:~/

SSH into your device:

ssh user@host.local

Move files where they need to go:

sudo mv myapp /usr/local/sbin/
sudo mv myapp.service /etc/systemd/system/

Enable system service:

sudo systemctl enable myapp --now

Restart your app if needed (e.g. after an update):

sudo systemctl restart myapp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment