All About systemd: Start, Stop, and Manage Services

All About systemd: Start, Stop, and Manage Services

systemd has become the de facto initialization system and service manager on most modern Linux distributions. It offers parallel startup, on-demand service activation, snapshotting, and sophisticated dependency resolution. This article explores every aspect of starting, stopping, enabling, and managing services under systemd.

1. Introduction to systemd

  • PID 1: systemd takes the first process slot (PID 1) and remains active until shutdown.
  • Unit Types: service, socket, target, timer, etc.
  • Dependency Management: Handles After=, Requires=, Wants=.
  • Parallelization: Starts multiple services simultaneously.

For official documentation, visit systemd Wiki.

2. Basic Commands with systemctl

Action Command
Start a service now systemctl start foo.service
Stop a service now systemctl stop foo.service
Enable on boot systemctl enable foo.service
Disable on boot systemctl disable foo.service
View status systemctl status foo.service
View logs journalctl -u foo.service

3. Managing Units and Dependencies

Units are configuration files that define how services start and stop. They reside in:

  • /etc/systemd/system/ (local overrides)
  • /run/systemd/system/ (runtime units)
  • /lib/systemd/system/ (package-supplied)

Key directives:

  • Description= Human-readable name
  • ExecStart= Program to run
  • After= Ordering dependency
  • Requires= Hard dependency
  • Wants= Soft dependency

3.1 Overriding Units

Create a drop-in file under /etc/systemd/system/foo.service.d/override.conf:

ltServicegt
  Restart=always
lt/Servicegt

Then reload: systemctl daemon-reload and restart service.

4. Targets and Boot Sequence

Targets are groupings of units. Examples:

  • multi-user.target: Normal non-graphical multi-user mode.
  • graphical.target: Multi-user GUI.
  • rescue.target: Single-user emergency mode.

Switch runlevels:

systemctl isolate rescue.target

5. Timers and Socket Activation

Timers can replace cron. Define foo.timer alongside foo.service:

  • [Timer]
    OnCalendar=-- 03:00:00
  • [Install]
    WantedBy=timers.target

Socket activation allows starting services on-demand when a socket is accessed, e.g., sshd.socket.

6. Logging and Troubleshooting

  • View logs for all units: journalctl -xe
  • Limit logs: journalctl -u foo.service --since 2024-01-01
  • Check for failed units: systemctl --failed

Enable persistent logs by creating /var/log/journal directory and restarting systemd-journald.

7. Advanced Management

  • Snapshotting: systemctl snapshot and later systemctl isolate [snapshot].
  • Resource Control: Integrates cgroups via directives like MemoryLimit=, CPUQuota=.
  • Masking: Prevent a service from being started:
    systemctl mask foo.service
  • Preset: Distribution-prescribed enabled/disabled state:
    systemctl preset foo.service

8. Use Cases and Examples

Example: Auto-restart a critical database:

ltUnitgt
  Description=MyDB Service
  After=network.target
lt/Unitgt
ltServicegt
  ExecStart=/usr/local/bin/mydb
  Restart=on-failure
  RestartSec=5
lt/Servicegt
ltInstallgt
  WantedBy=multi-user.target
lt/Installgt

9. Best Practices

  1. Never edit package-supplied units use overrides.
  2. Group related units under a custom .target.
  3. Keep unit files minimal delegate complex logic to scripts.
  4. Use systemctl daemon-reload after every change.

10. VPN Services and systemd

Integrating VPNs with systemd ensures secure networking on boot. Below are popular solutions:

Example unit for OpenVPN:

ltUnitgt
  Description=OpenVPN tunnel for private network
  After=network.target
lt/Unitgt
ltServicegt
  ExecStart=/usr/sbin/openvpn --config /etc/openvpn/client.conf
  Restart=always
  RestartSec=10
lt/Servicegt
ltInstallgt
  WantedBy=multi-user.target
lt/Installgt
Further Resources

© 2024 Comprehensive Linux Guides. All rights reserved.

Download TXT




Leave a Reply

Your email address will not be published. Required fields are marked *