On setting up a systemd service
Written 4 days ago
Say we have a long-running program foo
that needs to run on boot and restart on failure. Here’s a quick low-down on how to set it up on systems that support systemd -
- Create a
foo.service
file:
[Unit]
Description=a foo service that does baz
[Service]
ExecStart=/usr/bin/foo
Restart=on-failure
[Install]
WantedBy=multi-user.target
- Follow the script:
# Verify correctness
systemd-analyze verify foo.service
# Copy it to systemd's dir
sudo cp foo.service /etc/systemd/system/
# Reload systemd units
sudo systemctl daemon-reload
# Start immediately, and run on boot
sudo systemctl enable foo --now
- Done!
To ensure things are running as they should, check the service status:
systemctl status -u foo
And the logs:
journalctl -u foo
Resources