Skip to content

Complete systemd Service Management Guide: From Custom Service Creation to Operations

Target Audience

  • Intermediate Linux server administrators (with basic command-line proficiency)

Key Points

  1. Create and register custom systemd services
  2. Configure automatic startup and log monitoring
  3. 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

StepContentSuccess Criteria
1Create service file.service file placed in /etc/systemd/system
2Register and start servicesystemctl status shows "active (running)"
3Configure auto-startup and logsAuto-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

SymptomCauseImmediate Fix
Service startup failureWrong executable pathVerify absolute path in ExecStart=
Permission errorInsufficient user privilegesSpecify appropriate user in User=
No auto-startupForgot enable settingExecute systemctl enable servicename
Advanced Configuration (Optimization) ### Environment Variables
[Service]
Environment="API_KEY=secret"
EnvironmentFile=/etc/myapp/config
### Multi-process Management
[Service]
Type=forking  
PIDFile=/var/run/myapp.pid
ExecStartPre=/usr/bin/mkdir -p /var/run/myapp
### Security Hardening
[Service]
PrivateTmp=true
NoNewPrivileges=true
ProtectSystem=strict
ReadWritePaths=/var/lib/myapp