What is Systemd?
Systemd is the init system and service manager for modern Linux distributions. It manages services (daemons), mounts, timers, and the entire boot process.
Essential Commands
# Start/stop/restart a service
sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx
sudo systemctl reload nginx # Reload config without restart
# Enable/disable at boot
sudo systemctl enable nginx
sudo systemctl disable nginx
# Check status
systemctl status nginx
systemctl is-active nginx
systemctl is-enabled nginxListing Services
# All loaded services
systemctl list-units --type=service
# Failed services
systemctl --failed
# All installed service files
systemctl list-unit-files --type=serviceCreating a Custom Service
Create /etc/systemd/system/myapp.service:
[Unit]
Description=My Application
After=network.target
[Service]
Type=simple
User=www-data
WorkingDirectory=/var/www/myapp
ExecStart=/usr/bin/node /var/www/myapp/server.js
Restart=always
RestartSec=5
Environment=NODE_ENV=production
[Install]
WantedBy=multi-user.targetsudo systemctl daemon-reload
sudo systemctl enable --now myappViewing Logs
# Logs for a specific service
journalctl -u nginx
# Follow logs in real time
journalctl -u nginx -f
# Logs since last boot
journalctl -u nginx -b
# Last 100 lines
journalctl -u nginx -n 100