Efficient Management Tool for Linux Services: systemctl

Efficient Management Tool for Linux Services: systemctl

systemctl is a control tool for the system and service manager systemd, used to control the system and service manager of systemd. It is the primary tool for managing services in modern Linux distributions.

In the Linux ecosystem, Systemd has been deployed in most standard Linux distributions, with only a few distributions yet to adopt it. Systemd is typically the parent process of all other daemons, but this is not always the case.

Basic Concepts

What is systemd

systemd is a system and service manager that runs as PID 1 and starts the rest of the system. It provides:

  • • Parallel service startup
  • • On-demand activation of daemons
  • • Dependency-based service control logic
  • • Snapshots and system state recovery

Unit Types

systemd uses units to manage resources, with common unit types including:

  • <span>.service</span> – System services
  • <span>.socket</span> – Inter-process communication sockets
  • <span>.device</span> – Devices recognized by the kernel
  • <span>.mount</span> – Filesystem mount points
  • <span>.automount</span> – Automatic mount points
  • <span>.swap</span> – Swap devices or files
  • <span>.target</span> – Unit groups
  • <span>.path</span> – Paths in the filesystem
  • <span>.timer</span> – systemd timers

Basic Commands

System Management

# Reboot the system
systemctl reboot

# Power off the system
systemctl poweroff

# Suspend the system
systemctl suspend

# Hibernate the system
systemctl hibernate

# Enter hybrid sleep mode (both hibernate and suspend)
systemctl hybrid-sleep

Service Management

# Start a service
systemctl start [service_name]

# Stop a service
systemctl stop [service_name]

# Restart a service
systemctl restart [service_name]

# Reload service configuration (without restarting)
systemctl reload [service_name]

# Check service status
systemctl status [service_name]

# Enable service to start at boot
systemctl enable [service_name]

# Disable service from starting at boot
systemctl disable [service_name]

# Check if service is enabled
systemctl is-enabled [service_name]

# Mask a service (prevent manual or automatic start)
systemctl mask [service_name]

# Unmask a service
systemctl unmask [service_name]

Unit File Management

# Edit a unit file
systemctl edit [unit_name]

# Show unit file content
systemctl cat [unit_name]

# Show unit dependencies
systemctl list-dependencies [unit_name]

# Reload modified unit files
systemctl daemon-reload

Advanced Usage

View System Status

# List all active units
systemctl list-units

# List all units (including inactive ones)
systemctl list-units --all

# List all failed units
systemctl --failed

# View system status summary
systemctl status

# View system boot time
systemctl --property=ActiveEnterTimestamp show systemd-udevd.service

Log Management

# View service logs
journalctl -u [service_name]

# Follow logs in real-time
journalctl -f -u [service_name]

# View logs for a specific time period
journalctl --since "2023-01-01 00:00:00" --until "2023-01-02 12:00:00"

# View kernel logs
journalctl -k

Resource Management

# Set memory limit for a service
systemctl set-property [service_name] MemoryMax=500M

# Set CPU weight
systemctl set-property [service_name] CPUWeight=50

# View service resource usage
systemd-cgtop

Scheduled Tasks

# List all timers
systemctl list-timers --all

# Manually run a timer
systemctl start [timer_name].timer

Troubleshooting

Common Issue Resolution

  1. 1. Service startup failure
systemctl status [service_name]
journalctl -xe
  1. 2. Dependency issues
systemctl list-dependencies [service_name]
  1. 3. Configuration errors
systemd-analyze verify [unit_file]
  1. 4. Startup performance analysis
systemd-analyze
systemd-analyze blame
systemd-analyze critical-chain [service_name]

Custom Service Example

Create a custom service <span>/etc/systemd/system/myapp.service</span>:

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

[Service]
Type=simple
User=myuser
WorkingDirectory=/opt/myapp
ExecStart=/usr/bin/python3 /opt/myapp/main.py
Restart=on-failure

[Install]
WantedBy=multi-user.target

Then enable and start the service:

systemctl daemon-reload
systemctl enable myapp
systemctl start myapp

Conclusion

systemctl is a core tool for modern Linux system management, mastering it can effectively manage system services, troubleshoot issues, and optimize system performance. By combining it with journalctl to view logs, you can gain a comprehensive understanding of system operation.

–END–If you find this useful, please follow, like, and share. If you need technical assistance, feel free to contact us. We look forward to your visit.

Leave a Comment