systemd
What It Is
systemd manages services, dependencies, targets, timers, and logs in most modern Linux distributions.
Basic Commands
systemctl status nginx
systemctl start nginx
systemctl stop nginx
systemctl restart nginx
systemctl reload nginx
systemctl enable nginx
systemctl disable nginx
systemctl is-enabled nginx
systemctl list-units --type=service
systemctl list-unit-files --type=service
Working with Unit Files
Common paths:
/etc/systemd/systemfor local overrides and custom units/usr/lib/systemd/systemor/lib/systemd/systemfor distribution packages
After changing a unit:
Useful Commands
systemctl cat nginx
systemctl show nginx
systemctl edit nginx
systemctl mask nginx
systemctl unmask nginx
systemctl list-dependencies multi-user.target
Journald
journalctl -u nginx
journalctl -u nginx -f
journalctl -u nginx --since "1 hour ago"
journalctl -xe
journalctl -b
Minimal Unit
[Unit]
Description=My App
After=network.target
[Service]
Type=simple
User=app
WorkingDirectory=/srv/myapp
ExecStart=/usr/local/bin/myapp
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
Practice
- For custom services, always define
Restart=and a clearWorkingDirectory. systemctl editis better than editing a vendor unit directly because it creates an override.- For failure analysis, you almost always need both
systemctl statusandjournalctl -u.