What is systemd?
systemd is more than just a service manager - it's a suite of basic building blocks for a Linux system. However, one of its primary functions is managing services, or daemons, that run in the background. These services can include everything from yoprimaryur SSH server to your database management system.
Unit Files: The Building Blocks
At the heart of systemd's service management are unit files. These are configuration files that describe how systemd should manage a service. While there are several types of unit files, we'll focus on service units (ending in .service).
A typical service unit file might look like this:
[Unit]
Description=My Custom Web Server
After=network.target
Requires=network.target
[Service]
Type=simple
ExecStart=/usr/local/bin/mywebserver
Restart=always
User=webserver
[Install]
WantedBy=multi-user.target
Key Service Management Commands
Here are the essential systemd commands for managing services:
# Start a service
sudo systemctl start nginx.service
# Stop a service
sudo systemctl stop nginx.service
# Restart a service
sudo systemctl restart nginx.service
# Reload configuration without stopping
sudo systemctl reload nginx.service
# Enable service to start at boot
sudo systemctl enable nginx.service
# Disable service from starting at boot
sudo systemctl disable nginx.service
# View detailed service status
sudo systemctl status nginx.service
# Check if service is running
sudo systemctl is-active nginx.service
# Check if service is enabled at boot
sudo systemctl is-enabled nginx.service
Advanced Features
Dependencies and Ordering
One of systemd's strengths is its sophisticated dependency management. Unit files can specify:
Requires
: Hard dependencies that must be activeWants
: Optional dependenciesAfter
/Before
: Ordering requirementsConflicts
: Services that cannot run simultaneously
Service Types
systemd supports various service types:
simple
: The main process is the serviceforking
: The service forks a child processoneshot
: The service exits after completionnotify
: The service sends a notification when readyidle
: The service starts after other jobs complete
Monitoring and Troubleshooting
systemd provides robust tools for monitoring and troubleshooting services:
Viewing Logs
# View service logs
journalctl -u nginx.service
# View logs since last boot
journalctl -u nginx.service -b
# Follow logs in real-time
journalctl -u nginx.service -f
Note: The journalctl logs are ordered from chronological order (from old time to current time)
Resource Control
systemd can manage service resource usage:
[Service]
CPUQuota=50%
MemoryLimit=1G
TasksMax=100
Best Practices
Always Use Full Unit Names: Include the .service suffix for clarity.
Utilize Environment Files: Keep configuration separate from unit files.
Set Appropriate Restart Policies: Choose between
no
,always
,on-success
, oron-failure
.Implement Resource Limits: Prevent runaway services from impacting system stability.
Document Unit Files: Use comments to explain complex configurations.
Additional Resources
systemd Official Documentation
man systemd.service
man systemd.unit
man systemctl