systemctl in Unix - GeeksforGeeks

10 Jul, 2020
systemctl is used to examine and control the state of “systemd” system and service manager.
systemd is system and service manager for Unix like operating systems(most of the distributions, not all). As the system boots up, the first process created, i.e. init process with PID = 1, is systemd system that initiates the userspace services.
Syntax :
systemctl [OPTIONS...] COMMAND [NAME...]

Part of output of systemctl command
systemctl

Output:
snap-core-3887.mount                                        loaded active mounted   Mount unit for core
snap-simplescreenrecorder-1.mount                           loaded active mounted   Mount unit for simplescreenrecorder
sys-fs-fuse-connections.mount                               loaded active mounted   FUSE Control File System
sys-kernel-config.mount                                     loaded active mounted   Configuration File System
sys-kernel-debug.mount                                      loaded active mounted   Debug File System
acpid.path                                                  loaded active running   ACPI Events Check
cups.path                                                   loaded active running   CUPS Scheduler
systemd-ask-password-plymouth.path                          loaded active waiting   Forward Password Requests to Plymouth Directory Watch
systemd-ask-password-wall.path                              loaded active waiting   Forward Password Requests to Wall Directory Watch
systemd-networkd-resolvconf-update.path                     loaded active waiting   Trigger resolvconf update for networkd DNS
init.scope                                                  loaded active running   System and Service Manager
session-1.scope                                             loaded active running   Session 1 of user mandeep

another example, if you want to suspend your system, use below command
systemctl suspend
or 
systemctl -i suspend

systemctl options :
-t, --type= : the argument in this case should be comma-separated list of unit types such as service and socket.
--state= :  this is used to show only specified states, for example --state=failed, will show only failed units
-a, --all:  show all units, regardless of their states
-r, --recursive:  while listing units, show units of local containers also
--show-types:  it shows types of sockets along with showing sockets.
--job-mode : this option controls how to deal with already queued jobs in case of queuing a new job.
--fail : similar to --job-mode=fail
-i, --ignore-inhibitors:  When you request system shutdown or sleep state, it ignores inhibitor locks
-q, --quite :  it suppresses printing of results of various commands and the hints about the truncated lines
--no-block : don't wait synchronously for process to finish.
--user : talk to service manager of calling user, instead of system.
--no-wall : don't send wall message before power-off, halt or reboot.
-f, --force : when used with enable override any conflicting symlinks.
--message= : When used with halt, poweroff, reboot, kexec, you can display a short message specifying reason of operation.
-n, --lines : When used with status, controls the no. of journal lines to show.
-H, --host= : execute operation remotely. 

Examples :1)
systemctl --fail

It will shows jobs which are in failed state, similar to –job-mode=fail
Output:This is part of complete output
UNIT                                                        LOAD   ACTIVE SUB       DESCRIPTION
proc-sys-fs-binfmt_misc.automount                           loaded active waiting   Arbitrary Executable File Formats File System Automount Poi
sys-devices-pci0000:00-0000:00:02.0-drm-card0-card0\x2deDP\x2d1-intel_backlight.device loaded active plugged   /sys/devices/pci0000:00/0000:00:
sys-devices-pci0000:00-0000:00:03.0-sound-card0.device      loaded active plugged   Haswell-ULT HD Audio Controller
sys-devices-pci0000:00-0000:00:1b.0-sound-card1.device      loaded active plugged   8 Series HD Audio Controller
sys-devices-pci0000:00-0000:00:1c.0-0000:01:00.0-net-enp1s0.device loaded active plugged   RTL8101/2/6E PCI Express Fast/Gigabit Ethernet contr
sys-devices-pci0000:00-0000:00:1c.3-0000:02:00.0-net-wlp2s0.device loaded active plugged   Wireless 3160 (Dual Band Wireless AC 3160)
sys-devices-pci0000:00-0000:00:1d.0-usb1-1\x2d1-1\x2d1.5-1\x2d1.5:1.0-bluetooth-hci0.device loaded active plugged   /sys/devices/pci0000:00/000
sys-devices-pci0000:00-0000:00:1f.2-ata1-host0-target0:0:0-0:0:0:0-block-sda-sda1.device loaded active plugged   ST500LT012-1DG142 ESP
sys-devices-pci0000:00-0000:00:1f.2-ata1-host0-target0:0:0-0:0:0:0-block-sda-sda10.device loaded active plugged   ST500LT012-1DG142 10
sys-devices-pci0000:00-0000:00:1f.2-ata1-host0-target0:0:0-0:0:0:0-block-sda-sda11.device loaded active plugged   ST500LT012-1DG142 PBR_Image

2) -i option, sometimes it happens when you try to execute command, for example systemctl suspend, it don’t get executed, this is possible when some operation like copying of items or burning of CD, in that system use inhibitor locks to make sure that these operations are successful, you can override these locks using -i options
For example:
systemctl -i suspend

systemctl commands :
1) list-units : It lists all the units
list-units [PATTERN...]

example:
systemctl list-units

part of Output:
sys-devices-pci0000:00-0000:00:1f.2-ata1-host0-target0:0:0-0:0:0:0-block-sda-sda9.device loaded active plugged   ST500LT012-1DG142 9
sys-devices-pci0000:00-0000:00:1f.2-ata1-host0-target0:0:0-0:0:0:0-block-sda.device loaded active plugged   ST500LT012-1DG142
sys-devices-platform-coretemp.0-hwmon-hwmon1.device         loaded active plugged   /sys/devices/platform/coretemp.0/hwmon/hwmon1
sys-devices-platform-dell\x2dlaptop-leds-dell::kbd_backlight.device loaded active plugged   /sys/devices/platform/dell-laptop/leds/dell::kbd_ba
sys-devices-platform-serial8250-tty-ttyS0.device            loaded active plugged   /sys/devices/platform/serial8250/tty/ttyS0
sys-devices-platform-serial8250-tty-ttyS1.device            loaded active plugged   /sys/devices/platform/serial8250/tty/ttyS1
sys-devices-platform-serial8250-tty-ttyS10.device           loaded active plugged   /sys/devices/platform/serial8250/tty/ttyS10
sys-devices-platform-serial8250-tty-ttyS11.device           loaded active plugged   /sys/devices/platform/serial8250/tty/ttyS11
sys-devices-platform-serial8250-tty-ttyS12.device           loaded active plugged   /sys/devices/platform/serial8250/tty/ttyS12

2) list-sockets: it lists sockets addresses ordered by listening address.
systemctl list-sockets

Output:
LISTEN                          UNIT                            ACTIVATES
/dev/rfkill                     systemd-rfkill.socket           systemd-rfkill.service
/run/acpid.socket               acpid.socket                    acpid.service
/run/snapd-snap.socket          snapd.socket                    snapd.service
/run/snapd.socket               snapd.socket                    snapd.service
/run/systemd/fsck.progress      systemd-fsckd.socket            systemd-fsckd.service
/run/systemd/initctl/fifo       systemd-initctl.socket          systemd-initctl.service
/run/systemd/journal/dev-log    systemd-journald-dev-log.socket systemd-journald.service
/run/systemd/journal/socket     systemd-journald.socket         systemd-journald.service
/run/systemd/journal/stdout     systemd-journald.socket         systemd-journald.service
/run/systemd/journal/syslog     syslog.socket                   rsyslog.service
/run/udev/control               systemd-udevd-control.socket    systemd-udevd.service
/run/uuidd/request              uuidd.socket                    uuidd.service
/var/run/avahi-daemon/socket    avahi-daemon.socket             avahi-daemon.service
/var/run/cups/cups.sock         cups.socket                     cups.service
/var/run/dbus/system_bus_socket dbus.socket                     dbus.service
audit 1                         systemd-journald-audit.socket   systemd-journald.service
kobject-uevent 1                systemd-udevd-kernel.socket     systemd-udevd.service

Similarly you can use list-timers to list timer units ordered by the time they elapse next.
3) start, stop, reload, restart : As their name specifies, they are used for starting, stopping, reloading or restarting a service respectively
start PATTERN...
reload PATTERN...
stop PATTERN...
restart PATTERN...

4) is-active PATTERN... : check whether any of the specified service are in active state, similarly is-failed can be used to determine units in failed state.
5) status [PATTERN...|PID...]] : Show terse runtime status information about one or more units
systemctl status

For output, see the below image
systemctl status output
6) show [PATTERN...|JOB...] : Show properties of one or more units, jobs, or the manager itself.
systemctl show

Output:
Version=229
Features=+PAM +AUDIT +SELINUX +IMA +APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ -LZ4 +SECCOMP +BLKID +ELFUTILS +KMO
Architecture=x86-64
FirmwareTimestampMonotonic=0
LoaderTimestampMonotonic=0
KernelTimestamp=Tue 2018-02-13 08:07:08 IST
KernelTimestampMonotonic=0
InitRDTimestampMonotonic=0
UserspaceTimestamp=Tue 2018-02-13 08:07:13 IST
UserspaceTimestampMonotonic=5502424
FinishTimestamp=Tue 2018-02-13 08:07:53 IST
FinishTimestampMonotonic=45214960
SecurityStartTimestamp=Tue 2018-02-13 08:07:13 IST

7) enable NAME...: enable one or more unit files or their instances, similarly disable can be used for disabling.
8) suspend
systemctl supend

It can be used to suspend the system, similarly there is option for hibernate, hybrid-sleep, reboot, poweroff and halt
9) kexec : It shutdowns the system and reboots it via kexec.
I’ve mentioned most used commands and options in this article, for more you can access linux man page of systemctl.
– Mandeep Singh
Akanksha_Rai
linux-command
Linux-misc-commands
Linux-Unix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
scp command in Linux with Examples
mv command in Linux with examples
Docker - COPY Instruction
SED command in Linux | Set 2
chown command in Linux with Examples
nohup Command in Linux with Examples
Named Pipe or FIFO with example C program
Thread functions in C/C++
uniq Command in LINUX with examples
Start/Stop/Restart Services Using Systemctl in Linux