Systemd usage

From wikinotes
Revision as of 01:05, 9 November 2022 by Will (talk | contribs) (→‎Logs)

Documentation

man systemctl https://man.archlinux.org/man/core/systemd/systemctl.1.en

Start/Stop

systemctl {list-units,list-timers,list-sockets}

systemctl {enable,start,stop,restart} myservice

systemctl {is-active,is-enabled} myservice

systemctl show \
  [-p property,...]  `# list specific properties` \
  [foo.service]      `# filter to unitfile` \

# list filepaths for each service
for service in $(systemctl list-units -t service,timer | head -n -6 | sed -n '2,$p'  | sed -E 's/^[^a-zA-Z0-9]+//' | awk -F' ' '{ print $1 }'); do
  systemctl show -P Id,FragmentPath "$service" | sed 'N;s/\n/ /'
done | column -t

Management

systemctl --system daemon-reload          # reload unitfiles
systemctl [--user] list-units -t target   # list all targets

Logs

journalctl \
  # target
  /usr/bin/program    # for specific executable
  -u foo.service      # for specific service
  -b                  # boot messages

  # functions
  -f                  # tail logs
  -e                  # jump to bottom of page

  # filters
  -p 7                # set loglevel 0-7 (priority)
  -g '.*foo$'         # grep logs
  -S,-U '2012-12-30'  # since/until (optional time '2012-12-30 18:00:00')