Mastering Linux Process Management with systemd
Learn how to control, monitor, and optimize Linux services and processes using systemd with practical command-line examples.
If you administer Linux servers long enough, you’ll inevitably need to start, stop, or debug services. While traditional init scripts still exist, modern distributions rely on systemd as the default service manager. Understanding how to handle processes with systemd is essential for keeping your system stable and your applications running smoothly.
Checking Service Status
The most common task is verifying whether a service is active. Use systemctl status to get a detailed snapshot:
sudo systemctl status nginx
This command shows the current state (active/inactive), the process ID, recent log entries, and whether the service is enabled to start on boot. For a quick check without the extra output, use:
systemctl is-active nginx
systemctl is-enabled nginx
The first returns active or inactive, the second returns enabled or disabled. This is perfect for scripting.
Starting, Stopping, and Restarting Services
Basic lifecycle management is straightforward:
sudo systemctl start nginx # Start immediately
sudo systemctl stop nginx # Stop immediately
sudo systemctl restart nginx # Stop then start
sudo systemctl reload nginx # Reload config without restart
Use reload when the service supports it (like Nginx or Apache) — it avoids killing active connections. To enable or disable automatic startup:
sudo systemctl enable nginx # Start on boot
sudo systemctl disable nginx # Do not start on boot
Combine enable with start in one step:
sudo systemctl enable --now nginx
Viewing and Filtering Processes with systemctl list-units
To see all active systemd units (services, sockets, mounts), run:
systemctl list-units
For a cleaner view of only services:
systemctl list-units --type=service --state=running
This lists every running service, its load state, and description. If you suspect a service is failing repeatedly, check its recent logs:
journalctl -u nginx --since "5 minutes ago"
The -u flag filters by unit name. Add -f to follow logs in real time:
journalctl -u nginx -f
Managing Custom Processes with Systemd Service Files
Not every process is managed by a package. You can create your own systemd service for a custom script or application. Create a file at /etc/systemd/system/myapp.service:
[Unit]
Description=My Custom Application
After=network.target
[Service]
ExecStart=/usr/local/bin/myapp
Restart=on-failure
User=myuser
Group=mygroup
[Install]
WantedBy=multi-user.target
After saving, reload systemd and start the service:
sudo systemctl daemon-reload
sudo systemctl start myapp
sudo systemctl enable myapp
Key directives include Restart=always for critical services, WorkingDirectory to set the working path, and EnvironmentFile to load environment variables from a file.
Conclusion
Systemd gives you fine-grained control over processes with simple, consistent commands. Whether you’re checking status, restarting a web server, or writing your own service file, mastering systemctl and journalctl will save you time and headaches. Start by running systemctl status on a few services today — you’ll quickly see how much insight it provides.