eldelto
Created: Updated:

Systemd Cheat Sheet

This is a small cheat sheet of commands I most commonly use with systemd and journald.

Services

Here is a list of the most relevant, service-related systemd commands:

  • systemctl list-unit-files - List unit files
  • systemctl list-units - List active units
  • systemctl start <service> - Start a service
  • systemctl status <service> - Check a service's status
  • systemctl stop <service> - Stop a service
  • systemctl restart <service> - Restart a service
  • systemctl enable <service> - Enable a service to start on system boot
  • systemctl disable <service> - Stop a service from starting on system boot
  • systemctl is-enabled <service> - Check if a service is enabled
  • systemctl edit <service> - Edit a service's unit file
  • systemctl daemon-reload - Reload changed unit files

And as they say a code example says a thousand words, here is the .service file of this very blog:

          [Unit]
          Description=Personal blog

          [Service]
          Type=simple
          ExecStart=/home/eldelto/workspace/core/bin/blog
          WorkingDirectory=/home/eldelto/workspace/core
          Environment=REPO_DESTINATION=/home/eldelto/workspace/gtd

          Restart=always
          RestartSec=3

          [Install]
          WantedBy=multi-user.target

User Services

By default if you configure a systemd service as root user, the service will also run as root which is often not what we want. Instead you can register services for a specific user.

#+begin_center By default user services are only started once the user logs in and are terminated again when the last session of the user has been closed.

Execute loginctl enable-linger to start service of the current user at boot time. #+end_center

The .service files should go into $HOME/.config/systemd/user/. All systemd commands are the same, we only have to add the --user flag. For example to enable a user-service we can run systemctl --user enable blog.service.

Timers

Timers are the systemd alternative to the ol' reliable cronjob. They take a little more effort to setup but come with the benefit of logging to journald for free.

To create a timer that periodically calls a bash script to deploy this blog we need two .service files:

  • core-deploy.service which will execute the deployment script
  • core-deploy.timer to trigger the previous service

The content of the .service file:

          [Unit]
          Description=Fetches changes from the core repository and restarts related services.
          Wants=core-deploy.timer

          [Service]
          Type=oneshot
          ExecStart=/home/eldelto/workspace/core-deploy.sh
          WorkingDirectory=/home/eldelto/workspace/core

          [Install]
          WantedBy=multi-user.target

And of the .timer file:

          [Unit]
          Description=Fetches changes from the core repository and restarts related services.
          Requires=core-deploy.service

          [Timer]
          OnCalendar=*-*-* *:00:00

          [Install]
          WantedBy=timers.target

And here are some timer-related commands:

  • systemctl list-timers - Shows a list of active timers
  • systemd-analyze calendar -- :00/30:00 - Verifies the given cron-like expression and shows the next scheduled invocation

System

Commands that act system-wide:

  • systemctl reboot - Restart your system
  • systemctl poweroff - Shutdown your system

Logging

The centralized logging service of systemd is called journald. It stores the output of every systemd unit in a binary log file that can be queried via journald.

Some useful flags:

  • -e - Jumps to the end of the log
  • -f - Live-tail the log
  • -k - Only display kernel logs
  • -u <unit> - Only display logs of the given unit
  • --user - Display logs of user services
  • --since <date-time> - Display logs since the given date-time
  • --until <date-time> - Display logs until the given date-time

In the following example we display all logs of the blog unit since midnight:

          journalctl --user -u blog --since "today"

Sources