Skip to main content

1. Managing & Listing Services

# Check status of a service
systemctl status nginx
systemctl status postgresql

# Start / Stop / Restart
sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx

# Reload config WITHOUT downtime (if the service supports it)
sudo systemctl reload nginx

# Restart only if the service is already running
sudo systemctl try-restart nginx
💡 Use reload over restart for nginx/postgres in production — it avoids dropping connections.
# List ALL services (all states)
systemctl list-units --type=service

# List only RUNNING services
systemctl list-units --type=service --state=running

# List only FAILED services
systemctl list-units --type=service --state=failed

# List ENABLED services (start on boot)
systemctl list-unit-files --type=service --state=enabled

# Suppress the pager (useful for scripting / piping)
systemctl list-units --type=service --no-pager

# Filter by keyword (e.g. find anything docker-related)
systemctl list-units --type=service | grep docker

# Group filter using glob pattern (e.g. all snap services)
systemctl list-units 'snap.*' --type=service

# Show only the service name column (clean output)
systemctl list-units --type=service --no-legend | awk '{print $1}'
💡 Reading the output columns:
ColumnMeaning
UNITService name (e.g. nginx.service)
LOADWhether systemd parsed the unit file
ACTIVEHigh-level state: active, inactive, failed
SUBLow-level state: running, exited, dead
DESCRIPTIONHuman-readable label

2. Debugging & Logs

# View logs for a specific service
journalctl -u nginx

# Follow live logs (like tail -f)
journalctl -u nginx -f

# Show logs since last boot only
journalctl -u nginx -b

# Show last 50 lines
journalctl -u nginx -n 50

# Show logs within a time range
journalctl -u nginx --since "2024-01-01" --until "2024-01-02"

# Check why a service failed
systemctl status nginx
journalctl -xe   # Full system log with explanations

3. Boot & Startup Behavior

# Enable a service to start automatically on boot
sudo systemctl enable nginx

# Disable autostart on boot
sudo systemctl disable nginx

# Check if a service is enabled
systemctl is-enabled nginx

# Check if a service is currently running
systemctl is-active nginx

# See ALL running services
systemctl list-units --type=service --state=running

# See FAILED services (great for debugging boot issues)
systemctl list-units --type=service --state=failed

# Reboot / shutdown via systemctl
sudo systemctl reboot
sudo systemctl poweroff

4. Writing a Custom .service File

Step 1 — Create the file:
sudo nano /etc/systemd/system/myapp.service
Step 2 — Paste this template:
[Unit]
Description=My Node.js App
After=network.target          # Start after network is ready

[Service]
Type=simple
User=ubuntu                   # Run as this user (not root!)
WorkingDirectory=/home/ubuntu/myapp
ExecStart=/usr/bin/node server.js
Restart=on-failure            # Auto-restart if it crashes
RestartSec=5                  # Wait 5s before restarting
Environment=NODE_ENV=production
StandardOutput=journal        # Logs go to journalctl
StandardError=journal

[Install]
WantedBy=multi-user.target    # Start at normal boot
Step 3 — Activate it:
sudo systemctl daemon-reload   # Tell systemd about the new file
sudo systemctl enable myapp
sudo systemctl start myapp

# Verify
systemctl status myapp
journalctl -u myapp -f

Quick Reference Summary

TaskCommand
Check service statussystemctl status <name>
Start servicesudo systemctl start <name>
Stop servicesudo systemctl stop <name>
Restart servicesudo systemctl restart <name>
Enable on bootsudo systemctl enable <name>
Disable on bootsudo systemctl disable <name>
View logs livejournalctl -u <name> -f
Reload after .service editsudo systemctl daemon-reload
List running servicessystemctl list-units --type=service --state=running
List failed servicessystemctl list-units --state=failed
Filter services by namesystemctl list-units --type=service | grep <keyword>