Skip to main content
Back to Category

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.

Read time: 12 min Server Management
systemdsystemctlservicelinuxjournalctlunit filedaemon

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

hljs bash
# 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

hljs bash
# 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

hljs bash
# 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
hljs bash
# 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:

hljs ini
[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 description
  • After: Which services this should start after
  • Requires: Mandatory dependencies
  • Wants: Optional dependencies

[Service] Section:

  • Type: Service type (simple, forking, oneshot, notify, dbus)
  • ExecStart: Command to start the service
  • ExecStop: Command to stop the service
  • Restart: Restart policy
  • User/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

hljs bash
# Create the unit file
sudo nano /etc/systemd/system/myapp.service
hljs ini
[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
hljs bash
# 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

TypeDescriptionUse Case
simpleMain process runs directlyNode.js, Python
forkingMain process forks and exitsNginx, Apache
oneshotOne-time taskStartup scripts
notifySends notification when readySystemd-aware apps

Log Management with journalctl

Systemd's logging system journald collects all service logs centrally.

hljs bash
# 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

hljs bash
# 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

hljs bash
# Always run this after modifying a unit file
sudo systemctl daemon-reload

Service Keeps Restarting

hljs bash
# 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.

hljs bash
# Create a timer unit file
sudo nano /etc/systemd/system/backup.timer
hljs ini
[Unit]
Description=Daily Backup Timer

[Timer]
OnCalendar=daily
Persistent=true

[Install]
WantedBy=timers.target
hljs bash
# 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.