systemd Service Management: Auto-Start on Boot, Self-Heal on Crash
This guide covers systemd service management essentials: using systemctl for start/stop/enable, interpreting service status, creating custom unit files with auto-restart, querying logs via journalctl, and troubleshooting common failures like missing daemon-reload or environment mismatches.
systemctl: Start, Stop, Enable in One Command
Using nginx as an example, the most valuable command is enable --now — it enables auto-start on boot and starts the service immediately in one step.
systemctl start nginx # start
systemctl stop nginx # stop
systemctl restart nginx # restart
systemctl reload nginx # reload config (no downtime)
systemctl status nginx # view status
systemctl enable --now nginx # enable auto-start + start now
systemctl disable nginx # disable auto-start
systemctl daemon-reload # required after editing unit files
systemctl list-units --type=service # list running servicesKey insight: enable only configures auto-start; it does not start the service immediately. Use enable --now to both enable and start.
Reading Service Status: Focus on Three Lines
Run systemctl status myapp and watch these three lines:
Loaded – unit file path; enabled means auto-start is set.
Active – active (running) is healthy; failed or inactive indicates a problem.
Main PID – main process ID, useful with ps or kill.
When status is abnormal, the last few lines of status show the failure reason; details can be dug with journalctl (see section 04).
Writing a Custom Service: Hand Your Program to systemd
Example: a Python web app run with Gunicorn. Three steps.
Step 1: Create the unit file
Create /etc/systemd/system/myapp.service with three sections:
[Unit]
Description=My Python Web App
After=network.target
[Service]
Type=simple
User=deploy
WorkingDirectory=/opt/myapp
EnvironmentFile=/opt/myapp/.env
ExecStart=/opt/myapp/venv/bin/gunicorn -w 4 -b 127.0.0.1:8000 app:app
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.targetCore value: Restart=always makes systemd automatically restart the process on crash – the essence of “handing off to systemd”.
Step 2: Reload and start
sudo systemctl daemon-reload
sudo systemctl enable --now myapp
sudo systemctl status myappCritical: After any change to a .service file, you must run daemon-reload; otherwise changes have no effect.
Step 3: Place the file correctly
Custom services go in /etc/systemd/system/ (highest priority, overrides package units). Package-provided units live in /usr/lib/systemd/system/ – do not edit those.
journalctl: Querying Service Logs
After handing off to systemd, program output goes to the journal. Use journalctl -u <service>:
journalctl -u myapp # all logs for the service
journalctl -u myapp -f # follow live (like tail -f)
journalctl -u myapp -n 100 # last 100 lines
journalctl -u myapp --since today # today's logs
journalctl -xe # system logs with context (common for troubleshooting)Four Common Failure Scenarios
① Service fails to start
Two first commands:
systemctl status myapp
journalctl -u myapp -n 50Common causes: wrong ExecStart path, User lacks file permissions, missing .env file, port already in use (check with ss -tlnp | grep 8000).
② Changed service file but nothing happens
Forgot daemon-reload (see section 03).
③ Service doesn't auto-start on boot
Verify enablement and check this boot's logs:
systemctl is-enabled myapp
journalctl -u myapp -b④ Command works manually but fails under systemd
Likely environment variable differences: your interactive shell loads .bashrc, but systemd provides a clean environment. Pass variables explicitly via Environment or EnvironmentFile.
Summary Cheat Sheet
Manage long-running programs: unit file + enable --now After config change: daemon-reload + restart View logs: journalctl -u xxx -f Crash self-healing: Restart=always Auto-start after reboot: enable A service is truly “production-ready” only if it survives a reboot and recovers from crashes automatically. Both must be “yes”.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
