Systemd Service Management: Control System Services
Master systemd for service management. Unit files, service control, timers, targets, socket activation, dependency management, logging.
On this page
Systemd is the modern system and service manager for Linux. It manages system startup, services (Apache, MySQL, etc), logging, and scheduled tasks. Understanding systemd is essential for modern server administration.
Systemd Overview
Systemd replaces (older systems):**
- Init scripts (system startup)
- Cron (scheduled tasks)
- Syslog (system logging)
Key features:**
- Service management: Start/stop services
- Auto-restart: Automatically restart crashed services
- Dependency handling: Start services in right order
- Resource limits: Limit CPU/memory per service
- Logging: Unified journaling system
- Timers: Replace cron for scheduling
Unit Files
Unit file locations:**
/etc/systemd/system/ # System services (admin-created)
/lib/systemd/system/ # Installed package services
/run/systemd/system/ # Runtime services
Unit file types:**
- .service: Executable services (Apache, MySQL)
- .timer: Scheduled tasks (like cron)
- .socket: Network sockets
- .target: System state (like runlevels)
Service Control
Essential service commands:**
systemctl start apache2 # Start service
systemctl stop apache2 # Stop service
systemctl restart apache2 # Stop and start
systemctl reload apache2 # Reload config (no interruption)
systemctl enable apache2 # Auto-start on boot
systemctl disable apache2 # Don't auto-start
systemctl status apache2 # Check status
systemctl is-active apache2 # Return status code only
List services:**
systemctl list-units --type=service # All services
systemctl list-units --type=service --all # Including stopped
systemctl list-units --type=service --failed # Failed services
Create Custom Service
Create /etc/systemd/system/myapp.service:**
[Unit]
Description=My Node.js Application
After=network.target
[Service]
Type=simple
User=www-data
WorkingDirectory=/var/www/myapp
ExecStart=/usr/bin/node /var/www/myapp/app.js
Restart=always
RestartSec=10
# Environment variables
Environment="NODE_ENV=production"
Environment="PORT=3000"
EnvironmentFile=/var/www/myapp/.env
[Install]
WantedBy=multi-user.target
Enable and start:**
systemctl daemon-reload # Load new unit file
systemctl enable myapp # Auto-start on boot
systemctl start myapp # Start now
systemctl status myapp # Verify running
Targets and Dependencies
Targets are system states:**
- multi-user.target: Multi-user mode (normal)
- graphical.target: GUI mode (desktop)
- rescue.target: Emergency mode
- halt.target: Shutdown
- reboot.target: Reboot
Dependencies in unit files:**
[Unit]
Requires=mysql.service # Must have MySQL running
After=mysql.service # Start after MySQL
Before=app.service # Start before app
# Or use Wants (soft dependency—doesn't fail if missing)
Wants=postgres.service
Systemd Timers (Cron)
Create timer unit /etc/systemd/system/backup.timer:**
[Unit]
Description=Daily backup timer
[Timer]
OnCalendar=daily # Every day at midnight
OnCalendar=*-*-* 02:00:00 # Specific time (2 AM)
OnBootSec=5min # Run 5 minutes after boot
Unit=backup.service # Service to run
[Install]
WantedBy=timers.target
Create corresponding service /etc/systemd/system/backup.service:**
[Unit]
Description=Daily backup job
After=network.target
[Service]
Type=oneshot
ExecStart=/usr/local/bin/backup.sh
Enable and verify:**
systemctl enable backup.timer
systemctl start backup.timer
systemctl list-timers # Show all timers
systemctl status backup.timer
journalctl -u backup.service -n 20 # View last runs
View Service Logs
View service logs (journalctl):**
journalctl -u apache2 # Last 10 logs for Apache
journalctl -u apache2 -n 50 # Show 50 recent entries
journalctl -u apache2 --since "1 hour ago" # Last hour
journalctl -u apache2 -f # Follow in real-time
# View by severity
journalctl -u apache2 -p err # Errors only
journalctl -u apache2 -p warning # Warnings and errors
Troubleshooting
Service won't start:**
# Check service status
systemctl status myapp
# View detailed errors
journalctl -u myapp -n 50
# Validate unit file syntax
systemd-analyze verify /etc/systemd/system/myapp.service
# Test manually (without systemd)
/var/www/myapp/app.js # Run directly to see errors
Service keeps restarting:**
- Check logs for crash reason:
journalctl -u service -n 50 - Fix the underlying issue (permissions, config, code)
- Remove Restart=always if you don't want auto-restart
Service not starting on boot:**
- Verify enabled:
systemctl is-enabled service - Re-enable:
systemctl enable service - Check dependencies started first:
systemctl list-dependencies service
Understanding systemd is now essential. It unifies service management, logging, and scheduling across Debian, Ubuntu, CentOS, RHEL, and others.
Related: Process management | Linux booting | Service creation | Journalctl logging
Need a developer-friendly server?
Use an UnderHost Cloud VPS for SSH, Git, Node.js, Python, Laravel, Docker, cron, and custom development workflows.





















