Skip to content

Linux Service Management

Commands for managing Linux services using systemd (systemctl) and traditional init.d service commands.

systemd Commands (Modern Linux)

Most modern Linux distributions use systemd for service management.

Quick Reference

TaskCommand
List all servicessystemctl list-units --type=service
Start servicesudo systemctl start <service>
Stop servicesudo systemctl stop <service>
Restart servicesudo systemctl restart <service>
Reload configsudo systemctl reload <service>
Enable on bootsudo systemctl enable <service>
Disable on bootsudo systemctl disable <service>
Check statussystemctl status <service>
View logsjournalctl -u <service>

List Services

bash
# List all active services
systemctl list-units --type=service

# List all services (active and inactive)
systemctl list-units --type=service --all

# List only running services
systemctl list-units --type=service --state=running

# List failed services
systemctl list-units --type=service --state=failed

# List enabled services
systemctl list-unit-files --type=service --state=enabled

Example: Find specific service

bash
# Search for docker service
systemctl list-units | grep docker.service

# Or using pattern matching
systemctl list-units --type=service | grep docker

Service Control

bash
# Start service
sudo systemctl start nginx

# Stop service
sudo systemctl stop nginx

# Restart service
sudo systemctl restart nginx

# Reload configuration without restart
sudo systemctl reload nginx

# Reload or restart if reload not available
sudo systemctl reload-or-restart nginx

Enable/Disable Services

bash
# Enable service to start on boot
sudo systemctl enable docker.service

# Disable service from starting on boot
sudo systemctl disable docker.service

# Enable and start service immediately
sudo systemctl enable --now docker.service

# Disable and stop service immediately
sudo systemctl disable --now docker.service

# Check if service is enabled
systemctl is-enabled docker.service

Service Status

bash
# Check service status
systemctl status nginx

# Check if service is active
systemctl is-active nginx

# Check if service is failed
systemctl is-failed nginx

# Show service properties
systemctl show nginx

# Show specific property
systemctl show nginx -p ActiveState

Example status output:

● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2024-01-15 10:30:45 UTC; 2h 15min ago
   Main PID: 1234 (nginx)
      Tasks: 5 (limit: 4653)
     Memory: 12.5M
     CGroup: /system.slice/nginx.service
             ├─1234 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
             └─1235 nginx: worker process

Service Logs

bash
# View service logs
journalctl -u nginx

# Follow logs in real-time
journalctl -u nginx -f

# Show last 50 lines
journalctl -u nginx -n 50

# Show logs since specific time
journalctl -u nginx --since "2024-01-15 10:00:00"

# Show logs from today
journalctl -u nginx --since today

# Show logs from last hour
journalctl -u nginx --since "1 hour ago"

# Show logs with priority level
journalctl -u nginx -p err  # error level
journalctl -u nginx -p warning  # warning level

Common Service Examples

Docker Service

bash
# List Docker service
sudo systemctl list-units | grep docker.service

# Start Docker
sudo systemctl start docker.service

# Stop Docker
sudo systemctl stop docker.service

# Enable Docker on boot
sudo systemctl enable docker.service

# Disable Docker on boot
sudo systemctl disable docker.service

# Check Docker status
sudo systemctl status docker.service

# View Docker logs
journalctl -u docker.service -f

SMB/Samba Service

bash
# Check Samba service status
sudo service smbd status

# Start Samba
sudo service smbd start

# Stop Samba
sudo service smbd stop

# Using systemctl
sudo systemctl status smbd
sudo systemctl start smbd
sudo systemctl stop smbd

Nginx Web Server

bash
# Start Nginx
sudo systemctl start nginx

# Stop Nginx
sudo systemctl stop nginx

# Restart Nginx
sudo systemctl restart nginx

# Reload configuration
sudo systemctl reload nginx

# Enable on boot
sudo systemctl enable nginx

# Check status
sudo systemctl status nginx

SSH Service

bash
# Status
sudo systemctl status ssh
sudo systemctl status sshd  # On some systems

# Start/Stop
sudo systemctl start ssh
sudo systemctl stop ssh

# Enable/Disable
sudo systemctl enable ssh
sudo systemctl disable ssh

Apache Web Server

bash
# Ubuntu/Debian
sudo systemctl start apache2
sudo systemctl stop apache2
sudo systemctl restart apache2
sudo systemctl reload apache2

# RHEL/CentOS
sudo systemctl start httpd
sudo systemctl stop httpd
sudo systemctl restart httpd

MySQL/MariaDB

bash
# MySQL
sudo systemctl start mysql
sudo systemctl stop mysql
sudo systemctl status mysql

# MariaDB
sudo systemctl start mariadb
sudo systemctl stop mariadb
sudo systemctl status mariadb

PostgreSQL

bash
# Start PostgreSQL
sudo systemctl start postgresql

# Stop PostgreSQL
sudo systemctl stop postgresql

# Status
sudo systemctl status postgresql

# Enable on boot
sudo systemctl enable postgresql

Legacy Service Commands (SysV Init)

For older systems or for compatibility:

Quick Reference

bash
# List all services
sudo service --status-all

# Start service
sudo service <service> start

# Stop service
sudo service <service> stop

# Restart service
sudo service <service> restart

# Status
sudo service <service> status

Examples

bash
# List all services with status
sudo service --status-all

# SMB service
sudo service smbd status
sudo service smbd start
sudo service smbd stop

# Nginx
sudo service nginx start
sudo service nginx stop
sudo service nginx restart

Output of service --status-all:

 [ + ]  apache2
 [ - ]  bluetooth
 [ + ]  cron
 [ + ]  docker
 [ - ]  mysql
 [ + ]  nginx

Legend:

  • [ + ] = running
  • [ - ] = stopped
  • [ ? ] = unknown status

System Power Management

bash
# Reboot system
sudo systemctl reboot
sudo reboot

# Shutdown system
sudo systemctl poweroff
sudo shutdown -h now
sudo poweroff

# Suspend system
sudo systemctl suspend

# Hibernate system
sudo systemctl hibernate

# Shutdown in 10 minutes
sudo shutdown -h +10

# Cancel scheduled shutdown
sudo shutdown -c

Advanced systemd Operations

Reload systemd Configuration

bash
# Reload systemd manager configuration
sudo systemctl daemon-reload

Use this after:

  • Creating new service files
  • Modifying existing service files
  • Adding or removing service files

Service Dependencies

bash
# Show service dependencies
systemctl list-dependencies nginx

# Show reverse dependencies (what depends on this service)
systemctl list-dependencies nginx --reverse

# Show all dependencies recursively
systemctl list-dependencies nginx --all

Mask and Unmask Services

bash
# Mask service (prevent it from being started)
sudo systemctl mask nginx

# Unmask service
sudo systemctl unmask nginx

# Check if service is masked
systemctl status nginx

Masked services cannot be started manually or automatically.

Service Timers

bash
# List all timers
systemctl list-timers

# Show timer details
systemctl status <timer-name>.timer

# Enable timer
sudo systemctl enable <timer-name>.timer

# Start timer
sudo systemctl start <timer-name>.timer

Creating Custom Services

Simple Service File

Create /etc/systemd/system/myapp.service:

ini
[Unit]
Description=My Application
After=network.target

[Service]
Type=simple
User=myuser
WorkingDirectory=/opt/myapp
ExecStart=/usr/bin/python3 /opt/myapp/app.py
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

Enable and Start

bash
# Reload systemd
sudo systemctl daemon-reload

# Enable service
sudo systemctl enable myapp.service

# Start service
sudo systemctl start myapp.service

# Check status
sudo systemctl status myapp.service

Troubleshooting Services

Service Won't Start

bash
# Check detailed status
sudo systemctl status <service>

# View logs
sudo journalctl -u <service> -n 50

# Check for errors
sudo journalctl -u <service> -p err

# Verify service file
sudo systemctl cat <service>

# Check configuration
sudo nginx -t  # For nginx
sudo apache2ctl configtest  # For Apache

Service Keeps Failing

bash
# View recent logs
sudo journalctl -u <service> --since "10 minutes ago"

# Follow logs in real-time
sudo journalctl -u <service> -f

# Check if port is already in use
sudo netstat -tlnp | grep <port>
sudo ss -tlnp | grep <port>

# Check file permissions
ls -la /path/to/service/files

Service Dependencies

bash
# Check what service depends on
systemctl list-dependencies <service>

# Check what depends on service
systemctl list-dependencies <service> --reverse

# If dependency fails, check that service
sudo systemctl status <dependency>

Monitoring Services

Watch Service Status

bash
# Monitor service status (updates every 2 seconds)
watch -n 2 'systemctl status nginx'

# Monitor multiple services
watch -n 2 'systemctl status nginx apache2 mysql'

Automated Monitoring Script

bash
#!/bin/bash
# check-services.sh

SERVICES=("nginx" "docker" "mysql")

for service in "${SERVICES[@]}"; do
    if systemctl is-active --quiet "$service"; then
        echo "✓ $service is running"
    else
        echo "✗ $service is NOT running"
    fi
done

See Also

External Resources

Released under the MIT License.