I run things on my server. I'd like to run them in the background, so they restart when the server restarts.
I was running them "in the background" with tmux, which I had to set up again every time the server restarted. This was annoying. So, here I run them as a service.
I had a vague knowledge of services, because some things I use are one, like nginx and murmur for mumble.
This assumes you are running a version of Linux that uses systemd
Services (e.g., example.service
) can be enabled, started, stopped, restarted, and status-viewed with (respectively)
sudo systemctl enable example.service
sudo systemctl start example.service
sudo systemctl stop example.service
sudo systemctl restart example.service
sudo systemctl status example.service
List all services and find specific ones
sudo systemctl
sudo systemctl | grep example.service
Service files are usually stored in
/etc/systemd/system
For an application stored in /root/go/webring-go/serve
that serves files over HTTP, an example service file is
[Unit]
Description=Webring using Go and TOML
After=network.target
[Service]
ExecStart=/root/go/webring-go/serve
WorkingDirectory=/root/go/webring-go
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
If the service is named webring.service
cp webring.service /etc/systemd/system/webring.service
sudo systemctl enable webring.service
sudo systemctl start webring.service
sudo systemctl status webring.service
- https://stackoverflow.com/questions/38739198/how-to-run-a-script-as-a-service-in-ubuntu
- https://unix.stackexchange.com/questions/478999/how-can-i-make-an-executable-run-as-a-service
- https://patrakov.blogspot.com/2011/01/writing-systemd-service-files.html
- https://www.nginx.com/resources/wiki/start/topics/examples/systemd/
- https://linuxhandbook.com/create-systemd-services/
- https://stackoverflow.com/questions/12486691/how-do-i-get-my-golang-web-server-to-run-in-the-background