Skip to content

⚙️ systemd Service Management

What is systemd?

Starting from CentOS 7 / Ubuntu 15, Linux uses systemd by default to replace the aging SysV init. It's the system's "master manager" — handling service startup, logging, scheduled tasks, mount points, and nearly all system management tasks.

💡 Tip: 💡 In a nutshell: systemctl is the command-line tool you use to talk to systemd. Manage services with systemctl start/stop/enable, view logs with journalctl.

Core Service Management Commands

bash
# Start/stop/restart/view status
systemctl start nginx
systemctl stop nginx
systemctl restart nginx
systemctl reload nginx       # Reload configuration (without interrupting the service)
systemctl status nginx       # View status + recent logs

 nginx.service - A high performance web server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled)
     Active: active (running) since Mon 2025-01-20 10:30:15 CST
   Main PID: 1234 (nginx)
      Tasks: 5 (limit: 4915)
     Memory: 12.3M
        CPU: 230ms
     CGroup: /system.slice/nginx.service
             ├─1234 nginx: master process /usr/sbin/nginx
             └─1235 nginx: worker process

# Enable/disable at boot
systemctl enable nginx
systemctl disable nginx

# Start + enable at boot simultaneously
systemctl enable --now nginx

# View all running services
systemctl list-units --type=service --state=running

# View all enabled services (auto-start at boot)
systemctl list-unit-files --type=service --state=enabled

Writing Custom Service Files

Your own applications can also be managed with systemd. For example, a Node.js backend:

bash
# Create the service file
cat > /etc/systemd/system/myapp.service << 'EOF'
[Unit]
Description=My Node.js Application
After=network.target

[Service]
Type=simple
User=www-data
WorkingDirectory=/opt/myapp
ExecStart=/usr/bin/node server.js
Restart=always
RestartSec=5
Environment=NODE_ENV=production
Environment=PORT=3000

# Security hardening
NoNewPrivileges=true
ProtectSystem=strict
ReadWritePaths=/opt/myapp/data

[Install]
WantedBy=multi-user.target
EOF

# Reload + start
systemctl daemon-reload
systemctl enable --now myapp
systemctl status myapp

💡 Tip: 💡 Common Unit types: .service — service (most common), .socket — socket activation (on-demand service startup), .timer — scheduled tasks (alternative to cron), .mount — mount points

journalctl Log Viewing

bash
# View logs for a specific service
journalctl -u nginx -f              # Real-time follow
journalctl -u nginx --since "1h ago" # Last 1 hour
journalctl -u nginx --since "2025-01-20" --until "2025-01-21"

# View boot logs
journalctl -b                       # Current boot
journalctl -b -1                    # Previous boot

# Filter by priority
journalctl -u nginx -p err          # Only errors and above
journalctl -u nginx -p warning      # Only warnings and above

# Disk usage (logs can be large!)
journalctl --disk-usage
Archived and active journals take up 1.2G in the file system.

# Clean up old logs (keep the most recent 500MB)
journalctl --vacuum-size=500M