Complete systemd Service Management Guide: From Custom Service Creation to Operations¶
Target Audience
- Intermediate Linux server administrators (with basic command-line proficiency)
Key Points¶
- Create and register custom systemd services
- Configure automatic startup and log monitoring
- Execute basic systemd troubleshooting procedures
Why This Matters Now¶
Modern Linux environments require stable operation of diverse programs including web applications, API servers, and batch processes. systemd provides integrated process management, automatic recovery, and log management, significantly improving operational efficiency.
Solution Overview¶
| Step | Content | Success Criteria |
|---|---|---|
| 1 | Create service file | .service file placed in /etc/systemd/system |
| 2 | Register and start service | systemctl status shows "active (running)" |
| 3 | Configure auto-startup and logs | Auto-starts after reboot, logs accessible via journalctl |
Step 1: Create Service File¶
systemd services are defined in .service files. From basic templates to practical configurations:
# /etc/systemd/system/myapp.service
[Unit]
Description=My Application Service
After=network.target
Wants=network-online.target
[Service]
Type=simple
User=myuser
WorkingDirectory=/opt/myapp
ExecStart=/opt/myapp/bin/server
ExecReload=/bin/kill -HUP $MAINPID
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
Key Configuration Parameters¶
- Type=simple: Standard service running in foreground
- User: Execute with dedicated user (security enhancement)
- Restart=always: Automatic restart on failure
- After/Wants: Dependency to start after network initialization
Step 2: Register and Start Service¶
Make systemd recognize the service file and start it:
# Reload service configurations
sudo systemctl daemon-reload
# Start the service
sudo systemctl start myapp
# Check status
sudo systemctl status myapp
# Successful output example
# ● myapp.service - My Application Service
# Loaded: loaded (/etc/systemd/system/myapp.service; disabled; vendor preset: enabled)
# Active: active (running) since Thu 2025-08-29 09:00:00 JST; 2min ago
Step 3: Configure Auto-startup and Log Monitoring¶
Set up automatic startup and log monitoring for operations:
# Enable auto-startup
sudo systemctl enable myapp
# Test reboot (optional)
sudo reboot
# Verify auto-startup after reboot
sudo systemctl status myapp
# Real-time log monitoring
sudo journalctl -u myapp -f
# Check past logs
sudo journalctl -u myapp --since "1 hour ago"
Common Issues and Solutions¶
| Symptom | Cause | Immediate Fix |
|---|---|---|
| Service startup failure | Wrong executable path | Verify absolute path in ExecStart= |
| Permission error | Insufficient user privileges | Specify appropriate user in User= |
| No auto-startup | Forgot enable setting | Execute systemctl enable servicename |
Advanced Configuration (Optimization)
### Environment Variables[Service]
Environment="API_KEY=secret"
EnvironmentFile=/etc/myapp/config
[Service]
Type=forking
PIDFile=/var/run/myapp.pid
ExecStartPre=/usr/bin/mkdir -p /var/run/myapp
[Service]
PrivateTmp=true
NoNewPrivileges=true
ProtectSystem=strict
ReadWritePaths=/var/lib/myapp