• Root/
  • Linux/
  • systemd
  • systemd is a system and service manager

    Management

    Executing service management commands without understanding consequences will break installation.

    systemd allows for user-level services (managed via --user modifier). Note that user services will only run when there's user session; session lingering is available via sudo loginctl enable-linger although to make systemctl --user to be available, following should be appended to .bashrc: export XDG_RUNTIME_DIR="/run/user/$UID" export DBUS_SESSION_BUS_ADDRESS="unix:path=${XDG_RUNTIME_DIR}/bus" Such services operate not from from /etc/systemd/ but from /home/$USER/.config/systemd/user/

    Logs

    Services

    Instructions how to add custom systemd service

    1. Create file /etc/systemd/system/SERVICE_NAME.service
      [Unit]
      After=network.target
      StartLimitIntervalSec=0
      StartLimitBurst=5
      Wants=SERVICE_NAME.timer
      
      [Service]
      Type=simple
      Restart=always
      RestartSec=1
      User=USER
      Group=GROUP
      ExecStart=/bin/bash PATH_TO_EXECUTABLE
      
      [Install]
      WantedBy=multi-user.target
    2. Enable service and start it via sudo systemctl enable --now SERVICE_NAME.service

    Timers

    Instructions how to make custom systemd service run on specified schedule (modern alternative to CRON scheduling)

    External sources: https://wiki.archlinux.org/title/Systemd/Timers

    1. Create file /etc/systemd/system/SERVICE_NAME.timer
      [Unit]
      Description=Run foo weekly
      Requires=SERVICE_NAME.service
      
      [Timer]
      Unit=SERVICE_NAME.service
      OnCalendar=weekly
      Persistent=true
      
      [Install]
      WantedBy=timers.target

      OnCalendar - every minute is *-*-* *:*:00, use systemd-analyze calendar "query" to check

    2. Enable and start timer via sudo systemctl enable --now SERVICE_NAME.timer

      This will run service out-of-schedule once, immediately.

    Last update: 2024-06-17