Methods of payment Abuse

Systemctl: A Comprehensive Guide to Linux Service Management

13.10.2025, 15:53

If you're managing Linux servers, encountering systemctl is inevitable. This utility has become the de facto standard for controlling system processes across most modern distributions. Let's explore how to leverage this tool effectively and avoid common pitfalls when working with system services.

Systemd's Role in Contemporary Linux Systems

When you power on a Linux machine, the first process to launch carries process ID 1—that's systemd. It orchestrates the entire system: from initializing core services to managing network connections and mount points.

Unlike its predecessors (such as SysV Init), systemd employs parallel execution for compatible components. Imagine: instead of waiting for each service to start sequentially, the system launches multiple independent processes simultaneously. The result—your server becomes operational significantly faster.

Systemctl serves as your control panel for systemd. All commands flow through it: from simple "start the web server" to complex operations involving dependencies and resource constraints.

Daily Service Operations

Status Diagnostics

The first skill to master—checking what's happening with a service right now:

$ systemctl status apache2

The command reveals:

  • Whether the service is running or stopped
  • Time elapsed since startup
  • Memory and CPU consumption
  • Recent log entries

Need just a quick "running/not running" check?

$ systemctl is-active apache2

You'll get a concise response: active or inactive.

Lifecycle Management

Launch a service immediately:

$ sudo systemctl start apache2

Halt a running process:

$ sudo systemctl stop apache2

Complete restart (terminating all connections):

$ sudo systemctl restart apache2

Graceful configuration reload without dropping connections:

$ sudo systemctl reload apache2

Critical Point: These actions are temporary. After server reboot, everything reverts to autostart configurations.

Automatic Startup at Boot

To launch a service alongside the system:

$ sudo systemctl enable apache2

Systemd creates necessary links in startup directories.

Remove from autostart:

$ sudo systemctl disable apache2

Check current autostart status:

$ systemctl is-enabled apache2

Pro Tip: Combined command enables autostart and launches the service immediately:

$ sudo systemctl enable --now apache2

The Unit Concept in Systemd

Systemd operates with the concept of "units"—an abstraction for any managed object. Services represent just one type of unit.

Unit Varieties

  • service — Background processes and daemons
  • socket — Network or local IPC sockets
  • device — Hardware devices
  • mount — Disk mount points
  • automount — On-demand filesystem mounting
  • target — Logical unit groups
  • timer — Task scheduler (modern cron replacement)

Navigating Units

All currently active units:

$ systemctl list-units

Absolutely all units, including disabled ones:

$ systemctl list-units --all

Services only:

$ systemctl list-units --type=service

Display problematic units:

$ systemctl list-units --state=failed

Examining Unit Configuration

Complete list of unit files in the system:

$ systemctl list-unit-files

Contents of a specific unit:

$ systemctl cat apache2.service

Discover the physical file location:

$ systemctl show -p FragmentPath apache2.service

Detailed Unit Analysis

Systemctl provides access to extensive information about each unit.

All service parameters:

$ systemctl show apache2

The output will be lengthy. Filter for specific parameters:

$ systemctl show apache2 -p Requires

What's required to launch this service:

$ systemctl list-dependencies apache2

Reverse relationship—which services depend on this one:

$ systemctl list-dependencies --reverse apache2

Unit Blocking

Sometimes you need to absolutely prevent a service from starting—even if someone attempts manual activation.

Complete blockade:

$ sudo systemctl mask apache2

Systemd creates a symbolic link to /dev/null, rendering the unit inaccessible.

Removing the block:

$ sudo systemctl unmask apache2

When This Matters: During migration to a different web server (say, from Apache to Nginx), you can mask the old service to eliminate accidental launches.

Modifying Unit Configuration

The Proper Editing Approach

Utilize the built-in mechanism:

$ sudo systemctl edit apache2

An editor opens for creating a drop-in file. Your settings persist in /etc/systemd/system/apache2.service.d/override.conf and survive package updates.

Editing the complete unit file:

$ sudo systemctl edit --full apache2

After any modifications, always run:

$ sudo systemctl daemon-reload

Where Unit Files Reside

System files (installed by packages):

/lib/systemd/system/ — don't touch these directly.

Custom settings (highest priority):

/etc/systemd/system/ — your changes belong here.

Temporary units:

/run/systemd/system/

Switching System States

Targets — The Runlevel Analogy

A target defines the set of active services for a specific operational mode:

  • poweroff.target — Shutdown
  • rescue.target — Recovery mode (single-user)
  • multi-user.target — Console multi-user mode
  • graphical.target — Graphical interface
  • reboot.target — Reboot

Which target is active by default:

$ systemctl get-default

Set a different default target:

$ sudo systemctl set-default multi-user.target

Switch immediately:

$ sudo systemctl isolate graphical.target

Power Management Commands

Shutdown:

$ sudo systemctl poweroff

Restart:

$ sudo systemctl reboot

Sleep mode (RAM):

$ sudo systemctl suspend

Hibernate (disk):

$ sudo systemctl hibernate

Combined mode:

$ sudo systemctl hybrid-sleep

Boot Time Optimization

Systemd maintains detailed startup statistics.

Overall boot duration:

$ systemd-analyze

Which services slow down boot:

$ systemd-analyze blame

Displays a sorted list with initialization time for each service.

Boot critical path:

$ systemd-analyze critical-chain

Visualizes the dependency chain that determined total startup time.

Optimization Practice: Identify services with long initialization times. Verify whether unnecessary ones can be disabled or configured for delayed start via socket activation.

Working with System Journal

Systemd centrally collects logs from all components. Access via journalctl:

Entire journal:

$ journalctl

Logs for a specific service:

$ journalctl -u apache2

Real-time mode (tail -f equivalent):

$ journalctl -u apache2 -f

Last 100 entries:

$ journalctl -u apache2 -n 100

Since current boot:

$ journalctl -b

For a defined period:

$ journalctl --since "2025-01-15" --until "2025-01-20"
$ journalctl --since "30 minutes ago"

Errors only:

$ journalctl -p err

Journal Size Management

Delete older than 2 weeks:

$ sudo journalctl --vacuum-time=14d

Keep maximum 1 GB:

$ sudo journalctl --vacuum-size=1G

Creating Custom Services

Unit File Structure

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

[Unit]
Description=My Custom Application Service
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
User=appuser
Group=appuser
WorkingDirectory=/opt/myapp
ExecStart=/opt/myapp/bin/start
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
RestartSec=5s
StandardOutput=journal
StandardError=journal

[Install]
WantedBy=multi-user.target

Section Breakdown:

[Unit] — General information

  • Description — Human-readable description
  • After — Start after specified units
  • Wants — Soft dependency (non-critical)

[Service] — Execution parameters

  • Type — Process type
  • User/Group — Execution identity
  • WorkingDirectory — Working directory
  • ExecStart — Launch command
  • Restart — Restart policy on failure

[Install] — System integration

  • WantedBy — Which target to include in

Type Options

  • simple — Process from ExecStart is the main one.
  • forking — Creates child process, parent exits.
  • oneshot — Executes and terminates (for scripts).
  • notify — Reports readiness via sd_notify.
  • idle — Waits for other tasks to complete.

Service Registration

$ sudo systemctl daemon-reload
$ sudo systemctl enable myservice
$ sudo systemctl start myservice
$ systemctl status myservice

Troubleshooting Issues

Service Won't Start

Examine details:

$ systemctl status myservice

Dive into logs:

$ journalctl -u myservice -n 200 --no-pager

Verify unit file syntax:

$ systemd-analyze verify /etc/systemd/system/myservice.service

Dependency Conflicts

Dependency tree:

$ systemctl list-dependencies myservice --all

Search for circular dependencies:

$ systemctl show -p After,Before,Requires,Wants myservice

Permission Problems

Verify whether the service user can execute the binary:

$ sudo -u appuser /opt/myapp/bin/start

Advanced Techniques

Resource Limitations

CPU time constraint:

$ sudo systemctl set-property myservice CPUQuota=30%

Memory limit:

$ sudo systemctl set-property myservice MemoryMax=512M

Resource consumption monitoring:

$ systemd-cgtop

Socket Activation

Service starts only upon first socket access. Create /etc/systemd/system/myapp.socket:

[Unit]
Description=My App Socket

[Socket]
ListenStream=9000
Accept=no

[Install]
WantedBy=sockets.target

The service remains inactive until a connection arrives on port 9000.

Replacing Cron with Timers

Create /etc/systemd/system/backup.timer:

[Unit]
Description=Daily Backup Timer

[Timer]
OnCalendar=*-*-* 02:00:00
Persistent=true
Unit=backup.service

[Install]
WantedBy=timers.target

Activation:

$ sudo systemctl enable backup.timer
$ sudo systemctl start backup.timer

View all timers:

$ systemctl list-timers --all

Production Recommendations

  1. Never edit files in /lib/systemd/system — use systemctl edit.
  2. Always run daemon-reload after manual modifications.
  3. Test changes via systemctl status before production deployment.
  4. Document custom units directly in Description fields.
  5. Use overrides rather than complete file copies.
  6. Monitor journals regularly.
  7. Limit resources for critical services.
  8. Apply principle of least privilege in User/Group settings.

Summary

Systemctl represents the Swiss Army knife of Linux administration. Its capabilities extend far beyond simple "start/stop" operations. Proficient systemd usage enables building reliable, fast-booting systems with predictable service behavior.

The transition to systemd once sparked controversy in the community, but today it's an industry standard. Parallel booting, intelligent dependency management, integrated logging, and resource control—all of this makes systemd indispensable for modern infrastructure.

Invest time in mastering systemctl—it pays dividends manifold during the first serious incident or optimization task you encounter.