Skip to content

Instantly share code, notes, and snippets.

@alifeee
Last active February 7, 2024 22:26
Show Gist options
  • Save alifeee/606ffd2cefaceeeff92a037e797e5458 to your computer and use it in GitHub Desktop.
Save alifeee/606ffd2cefaceeeff92a037e797e5458 to your computer and use it in GitHub Desktop.
How to run things as a service

How to run things as a service on Ubuntu

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.

Firstly

This assumes you are running a version of Linux that uses systemd

Common service commands

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

Where services are described

Service files are usually stored in

/etc/systemd/system

An example service file

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

How to enable

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

Sources

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