Archive · Linux

Managing Services with systemd

Start, stop and enable services, read their logs, and write a simple unit file.

1 min read

  • Ubuntu 18.04.3 LTS
  • systemd 237
  • CentOS 7.7
  • systemd 219

Both Ubuntu 18.04 (systemd 237) and CentOS 7 (systemd 219) use systemd to start and supervise services.

sudo systemctl status nginx
sudo systemctl enable --now nginx     # start now and at every boot
sudo systemctl restart nginx
journalctl -u nginx --since "1 hour ago"
systemctl list-units --failed

A minimal unit for your own application, saved as /etc/systemd/system/myapp.service:

[Unit]
Description=My app
After=network.target

[Service]
User=app
WorkingDirectory=/opt/myapp
ExecStart=/opt/myapp/run.sh
Restart=on-failure

[Install]
WantedBy=multi-user.target

Then sudo systemctl daemon-reload && sudo systemctl enable --now myapp. Restart=on-failure gives you crash recovery without any extra tooling.