systemd: Managing Services in Linux

systemd is the init system and system manager widely adopted in modern Linux distributions. It initializes and manages system processes after the kernel boots, taking over from older SysVinit and Upstart systems. systemd uses “unit files” to define and manage various system resources, including services, mount points, devices, sockets, and more. This document focuses on creating and managing systemd service unit files.

1. systemd Service Unit File Example

A service unit file typically resides in /etc/systemd/system/ or /usr/lib/systemd/system/ and defines how a service should be managed by systemd.

Example: nfsen.service

# /etc/systemd/system/nfsen.service (or similar path)
 
[Unit]
# Description of the service (displayed in 'systemctl status')
Description=nfsen
# Defines dependencies: this service starts only after 'network-online.target' is active
# (ensures network is up and configured)
After=network-online.target
 
[Service]
# Defines the service type
# Type=simple: default, process specified by ExecStart is the main process
# Type=forking: process forks into a background daemon
# Type=oneshot: process runs once and then exits, systemd waits for it to finish
Type=simple
# If set to yes for Type=oneshot, systemd considers the service still active after ExecStart exits
# (useful for scripts that perform a task and exit)
# RemainAfterExit=yes
# Command to start the service
ExecStart=/data/nfsen/bin/nfsen start
# Command to stop the service
ExecStop=/data/nfsen/bin/nfsen stop
# Command to reload the service configuration
ExecReload=/data/nfsen/bin/nfsen restart
 
[Install]
# Defines when the service should be started (e.g., in multi-user.target, which is the normal runlevel)
WantedBy=multi-user.target

2. Key Sections and Directives

[Unit] Section

Contains generic options about the unit, largely independent of the type of unit.

  • Description: A brief description of the unit.
  • Documentation: URLs pointing to documentation for the unit.
  • After, Before: Defines ordering dependencies. After=network-online.target means this unit starts after network-online.target is active.
  • Requires, Wants: Defines stronger or weaker dependencies.

[Service] Section

Contains service-specific options, only applicable to service units.

  • Type: Defines the process startup type (e.g., simple, forking, oneshot, dbus, notify, idle).
  • ExecStart: The command to execute to start the service.
  • ExecStop: The command to execute to stop the service.
  • ExecReload: The command to execute to reload the service’s configuration.
  • Restart: Defines when the service should be restarted (e.g., always, on-failure, no).
  • RemainAfterExit: If yes, the service is considered active even after ExecStart exits.

[Install] Section

Contains installation information for systemctl enable.

  • WantedBy: Defines the target unit (e.g., multi-user.target) under which this service should be enabled. When enabled, systemd creates a symlink from the .service file to the .wants directory of the target.

3. Managing systemd Services

Once you’ve created or modified a service unit file, use systemctl to manage it.

1. Reload systemd Daemon

After creating or modifying a unit file, systemd needs to reload its configuration.

sudo systemctl daemon-reload

2. Start the Service

sudo systemctl start <service_name>.service
# Example:
sudo systemctl start nfsen.service

3. Stop the Service

sudo systemctl stop <service_name>.service

4. Restart the Service

sudo systemctl restart <service_name>.service

5. Check Service Status

sudo systemctl status <service_name>.service

6. Enable Service (Start on Boot)

To ensure the service starts automatically after each reboot.

sudo systemctl enable <service_name>.service

7. Disable Service (Prevent Start on Boot)

sudo systemctl disable <service_name>.service

8. View Service Logs

Check the service’s logs using journalctl.

sudo journalctl -u <service_name>.service -f
  • -u <service_name>.service: Filters logs for the specified unit.
  • -f: Follows the log output in real-time.