Systemd Service Management: start, stop, enable Guide
Complete guide to systemctl commands, service unit files, creating custom services, journalctl log monitoring, and systemd troubleshooting on Linux servers.
Table of Contents
Systemd Service Management: start, stop, enable Guide
Systemd is the init system and service manager used by most modern Linux distributions. On Ubuntu, Debian, CentOS, AlmaLinux, and RHEL, systemd handles everything from system startup to service management. This guide covers systemctl commands, unit files, and creating custom services.
What is Systemd?
Systemd is the first process started after the Linux kernel (PID 1). Its responsibilities include:
- Starting and stopping system services
- Managing dependencies between services
- Collecting system logs (journald)
- Monitoring system state
Systemd replaced the older SysV init system. While /etc/init.d/ scripts still work, systemd unit files are now the preferred approach.
Basic systemctl Commands
Checking Service Status
# View service status
sudo systemctl status nginx
# Check if service is running
sudo systemctl is-active nginx
# Check if service is enabled
sudo systemctl is-enabled nginx
# List all running services
sudo systemctl list-units --type=service --state=running
# List failed services
sudo systemctl list-units --type=service --state=failed
Starting and Stopping Services
# Start a service
sudo systemctl start nginx
# Stop a service
sudo systemctl stop nginx
# Restart a service (stop + start)
sudo systemctl restart nginx
# Reload service configuration without stopping
sudo systemctl reload nginx
# Reload if possible, otherwise restart
sudo systemctl reload-or-restart nginx
Enabling and Disabling Services
# Enable service to start at boot
sudo systemctl enable nginx
# Start service and enable at boot
sudo systemctl enable --now nginx
# Disable automatic startup
sudo systemctl disable nginx
# Stop service and disable
sudo systemctl disable --now nginx
# Mask a service (prevent it from being started)
sudo systemctl mask nginx
# Unmask a service
sudo systemctl unmask nginx
Systemd Unit Files
Unit files define how systemd manages services. There are two main locations:
/lib/systemd/system/— Unit files installed by package manager/etc/systemd/system/— Unit files created/customized by system administrator
# View a service's unit file
sudo systemctl cat nginx
# Find the unit file location
sudo systemctl show nginx --property=FragmentPath
# List all unit files
sudo systemctl list-unit-files --type=service
Unit File Structure
A .service unit file has three main sections:
[Unit]
Description=Nginx HTTP Server
After=network.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[Unit] Section:
Description: Service descriptionAfter: Which services this should start afterRequires: Mandatory dependenciesWants: Optional dependencies
[Service] Section:
Type: Service type (simple, forking, oneshot, notify, dbus)ExecStart: Command to start the serviceExecStop: Command to stop the serviceRestart: Restart policyUser/Group: User to run the service as
[Install] Section:
WantedBy: Which target this belongs to
Creating Custom Services
Creating a systemd service for your own application is straightforward.
Example: Node.js Application Service
# Create the unit file
sudo nano /etc/systemd/system/myapp.service
[Unit]
Description=My Node.js Application
After=network.target
[Service]
Type=simple
User=www-data
WorkingDirectory=/var/www/myapp
ExecStart=/usr/bin/node /var/www/myapp/app.js
Restart=on-failure
RestartSec=10
StandardOutput=journal
StandardError=journal
Environment=NODE_ENV=production
Environment=PORT=3000
[Install]
WantedBy=multi-user.target
# Reload systemd to recognize the new unit file
sudo systemctl daemon-reload
# Start and enable the service
sudo systemctl enable --now myapp
# Check status
sudo systemctl status myapp
Service Types Explained
| Type | Description | Use Case |
|---|---|---|
simple | Main process runs directly | Node.js, Python |
forking | Main process forks and exits | Nginx, Apache |
oneshot | One-time task | Startup scripts |
notify | Sends notification when ready | Systemd-aware apps |
Log Management with journalctl
Systemd's logging system journald collects all service logs centrally.
# View all logs
sudo journalctl
# View logs for a specific service
sudo journalctl -u nginx
# View last 50 lines
sudo journalctl -u nginx -n 50
# Follow logs in real time
sudo journalctl -u nginx -f
# Logs from the last hour
sudo journalctl -u nginx --since "1 hour ago"
# Specific date range
sudo journalctl -u nginx --since "2024-01-01" --until "2024-01-02"
# Only error logs
sudo journalctl -u nginx -p err
# Boot logs
sudo journalctl -b
# Check log disk usage
sudo journalctl --disk-usage
# Clean up old logs (older than 2 weeks)
sudo journalctl --vacuum-time=2weeks
Troubleshooting
Service Won't Start
# Get detailed status
sudo systemctl status myapp -l
# Check recent logs
sudo journalctl -u myapp -n 100 --no-pager
# Validate unit file
sudo systemd-analyze verify /etc/systemd/system/myapp.service
# Check dependency issues
sudo systemctl list-dependencies myapp
Forgot to Run daemon-reload
# Always run this after modifying a unit file
sudo systemctl daemon-reload
Service Keeps Restarting
# Check restart policy
sudo systemctl show myapp --property=Restart
# Reset the failure counter
sudo systemctl reset-failed myapp
When developing a service, start with Type=simple and Restart=on-failure. Optimize other settings once the service is stable.
Systemd Timers (Cron Alternative)
Systemd timers offer a modern alternative to cron jobs.
# Create a timer unit file
sudo nano /etc/systemd/system/backup.timer
[Unit]
Description=Daily Backup Timer
[Timer]
OnCalendar=daily
Persistent=true
[Install]
WantedBy=timers.target
# Enable the timer
sudo systemctl enable --now backup.timer
# List all timers
sudo systemctl list-timers
Conclusion
Systemd is the cornerstone of modern Linux server management. Learning systemctl commands and unit file structure lets you reliably manage services on your server. On REXE servers, you can create custom services to ensure your applications automatically start after system reboots.
Related Articles
How to Connect to Your VDS Server via SSH (Complete Guide)
Step-by-step SSH connection guide for Linux VDS: PuTTY, Terminal, key-based authentication, Path Panel firewall rules, and security best practices.
How to Connect to Windows VDS via RDP: Complete Guide
Complete guide to connecting to Windows Server VDS via Remote Desktop (RDP): setup steps, NLA security, Path Panel firewall rule, and troubleshooting tips.
How to Change VDS Server OS: Step-by-Step Reinstall Guide
Step-by-step guide to changing your VDS server OS via REXE panel: backup tips, supported Linux distributions and Windows Server versions explained.