YannStatic
  • Etiquettes
  • Liens
  • Aide

Linux - Systemd cheatsheet

  • systemd
  •  29 nov.  2020
Afficher/cacher Sommaire

systemctl

Activates a service immediately:

systemctl start foo.service

Deactivates a service immediately:

systemctl stop foo.service

Restarts a service:

systemctl restart foo.service

Shows status of a service including whether it is running or not:

systemctl status foo.service

Enables a service to be started on bootup:

systemctl enable foo.service

Disables a service to not start during bootup:

systemctl disable foo.service

Check whether a service is already enabled or not:

0 indicates that it is enabled. 1 indicates that it is disabled

systemctl is-enabled foo.service; echo $?

Change ad-hoc runlevel with systemctl isolate command:

Switch to another target (in this case multi-user/runlevel 3 in old SysV):

systemctl isolate multi-user.target

Switch to graphical target (in this case graphical/runlevel 5 in old SysV):

systemctl isolate graphical.target

Change permanently change default.target:

Remove configured default target

rm /etc/systemd/system/default.target

Create default.target as symbolic link to multi-user.target

ln -sf /lib/systemd/system/multi-user.target /etc/systemd/system/default.target

Set default target through systemctl

To find a [new target]:

- To get active targets, 
  systemctl --type=target list-units
- To get all targets, (active/inactive)
  systemctl --type=target --all list-units
- To list available targets, (ls -l /usr/lib/systemd/system/*.target)
  systemctl --type=target list-unit-files


systemctl set-default [new target]

List-units by pattern:

systemctl list-units *etwok*.service

Example content:
UNIT                   LOAD   ACTIVE SUB     DESCRIPTION
network.service        loaded active exited  LSB: Bring up/down networking
NetworkManager.service loaded active running Network Manager

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.

2 loaded units listed. Pass --all to see loaded but inactive units, too.

Display a content of unit file

systemctl cat network.service

Displayed content:

# /run/systemd/generator.late/network.service
# Automatically generated by systemd-sysv-generator
[Unit]
Documentation=man:systemd-sysv-generator(8)
SourcePath=/etc/rc.d/init.d/network
Description=LSB: Bring up/down networking
Before=runlevel2.target runlevel3.target runlevel4.target runlevel5.target shutdown.target network-online.target network.target
After=iptables.service ip6tables.service NetworkManager-wait-online.service NetworkManager.service
Wants=network-online.target
Conflicts=shutdown.target
[Service]
Type=forking
Restart=no
TimeoutSec=5min
IgnoreSIGPIPE=no
KillMode=process
GuessMainPID=no
RemainAfterExit=yes
ExecStart=/etc/rc.d/init.d/network start
ExecStop=/etc/rc.d/init.d/network stop
ExecReload=/etc/rc.d/init.d/network reload

Suppress non zero exit code (service after stop will be displayed as inactive, not failed):

[Service]
SuccessExitStatus=143

Link:

  • services-remain-in-failed-state-after-stopped-with-systemctl

How to put PID of service as variable to service systemd file:

ExecReload=/bin/kill -s HUP $MAINPID

Link

  • you-should-be-using-pidfile-and-mainpid-instead-of-pkill-1935e4531931
  • https://www.freedesktop.org/software/systemd/man/systemd.service.html#ExecStop=

List all services with pattern

<pattern> - httpd e.g

systemctl list-units -t service --full | grep <pattern> | sed 's/^\s*//g' | cut -d " " -f1 | while read s; do systemctl status $s; done

Link

  • systemd-list-units-cuts-service-names

List all enable units

systemctl list-unit-files | grep enabled

smartd.service                                enabled 
sshd.service                                  enabled 
sysstat.service                               enabled 
...

Link

  • how-to-list-all-enabled-services-from-systemctl

Reference links:

  • systemd.service man
  • Useful SystemD commands
  • Archlinux systemd wiki
  • Red Hat RHEL 7 systemd documentation
  • difference-between-systemd-and-terminal-starting-program
  • where-do-i-put-my-systemd-unit-file
  • trouble-creating-pid-file-in-systemd-service-script
  • Fedora - Packaging:Systemd
  • controlling-a-multi-service-application-with-systemd
  • rhel7-use-systemd-timers
  • how-to-remove-systemd-services
  • systemd-containers-introduction-systemd-nspawn
  • archlinux systemd-nspawn
  • sd_notify python watchdog implementation
  • sd_notify python service notify implemetation
  • python sdnotify library
  • creating-a-linux-service-with-systemd
  • systemd.io
  • systemd-ignores-return-code-while-starting-service
  • how-do-i-override-or-configure-systemd-services
  • creating-a-linux-service-with-systemd
  • An example network service with systemd-activated socket in Python
  • how-can-i-send-a-message-to-the-systemd-journal-from-the-command-line
  • systemd-forking-vs-simple
  • how-can-a-systemd-service-flag-that-is-is-ready-so-that-other-services-can-wait

Pid Eins:

  • systemd-for-admins-I
  • systemd-for-admins-II
  • systemd-for-admins-II
  • systemd-for-admins-IV
  • systemd-for-admins-V
  • systemd-for-admins-VI
  • systemd-for-admins-VII
  • systemd-for-admins-VIII
  • systemd-for-admins-IX
  • systemd-for-admins-X
  • systemd-for-admins-XI
  • systemd-for-admins-XII
  • systemd-for-admins-XIII
  • systemd-for-admins-XIV
  • systemd-for-admins-XV
  • systemd-for-admins-XVI
  • systemd-for-admins-XVII
  • systemd-for-admins-XVIII
  • systemd-for-admins-XIX
  • systemd-for-admins-XX

Digital Ocean:

  • systemd-essentials-working-with-services-units-and-the-journal
  • How-to-use-systemctl-to-manage-systemd-services-and-units
  • Understanding-systemd-units-and-unit-files
PRÉCÉDENTPC1 Ordinateur Bureau ArchLinux xfce (NEW)
SUIVANTPC1 ArchLinux - VM Debian 10 vdb (jekyll-statique)
Recherche