Chapter 10: Service Controller

Chapter 10: systemd Services

Service Control

sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx
sudo systemctl reload nginx
sudo systemctl status nginx
sudo systemctl enable nginx
sudo systemctl disable nginx

Listing Services

systemctl list-units --type=service
systemctl list-units --state=failed

Logs with journalctl

journalctl -u nginx
journalctl -u nginx -f
journalctl -p err
journalctl --since today

Custom Service Unit

[Unit]
Description=My App
After=network.target
[Service]
ExecStart=/opt/app/start.sh
Restart=on-failure
[Install]
WantedBy=multi-user.target

sudo systemctl daemon-reload

Notes

  • systemd replaced SysV init
  • Targets replace runlevels
  • daemon-reload required after unit file changes