Chapter 8. Process Management

Systemctl Sub-Commands

Systemctl Sub-Commands
Tag:

Systemctl is a command-line tool used to monitor and control systemd and units. It has several sub-commands used to show the statuses of unit files and active units or control and monitor specific units.

Commands for controlling a specific unit

There are sub-commands to control a specific unit (or unit file). If you are not logged in as the superuser, add the sudo prefix to the command.

systemctl start [unit name]

When you want to start a unit named unit_a.service, use this command.

Command Line - INPUT
sudo systemctl start unit_a

systemctl stop [unit name]

When you want to stop a running unit, use this command.

Command Line - INPUT
sudo systemctl stop unit_a

systemctl restart [unit name]

When you want to restart a running unit, use this command.

Command Line - INPUT
sudo systemctl restart unit_a

systemctl reload [unit name]

When you update a unit file and reload it, use this command.

Command Line - INPUT
sudo systemctl reload unit_a

systemctl enable [unit name]

When you want to start a unit (unit_a) automatically at boot, you need to enable it with this sub-command. When you run this command, a symbolic link of unit_a's unit file is created under a related directory of the unit that has a dependency with unit_a (typically, under a directory ending with .wants). This symbolic link is used to start unit_a.

Command Line - INPUT
sudo systemctl enable unit_a
Command Line - RESPONSE
Created symlink /etc/systemd/system/multi-user.target.wants/unit_a.service → /etc/systemd/system/unit_a.service.

With --now option, you can also start the unit when you run the command.

Command Line - INPUT
sudo systemctl enable --now unit_a

systemctl disable [unit name]

When you don't want to start the unit (unit_a) automatically at boot anymore, you can disable it with this sub-command. When you run this command, the symbolic link under the directory (ending with .wants) is removed.

Command Line - INPUT
sudo systemctl disable unit_a
Command Line - RESPONSE
Removed /etc/systemd/system/multi-user.target.wants/unit_a.service.

With --now option, you can also stop the unit when you run the command.

Command Line - INPUT
sudo systemctl disable --now unit_a

systemctl status [unit name]

When you want to check the status of a specific unit, such as ‘active(running)’ or ‘stopped’ and ‘enabled’ or ‘disabled’, you can use this sub-command. For this command, the sudo privilege is not necessary.

Command Line - INPUT
systemctl status unit_a
Command Line - RESPONSE
 unit_a.service - unit description
     Loaded: loaded (/etc/systemd/system/unit_a.service; enabled; vendor preset: >
     Active: active (running) since Sat 2023-01-14 02:45:18 UTC; 11s ago
   Main PID: 103219 (unit_a.)
      :

The status information is shown with a pager. To exit the pager mode, press the q key.

General control commands (not for a specific unit)

There are sub-commands that are not used for a specific unit, such as systemctl list-unit-files and systemctl list-unit, which are explained on the systemd topic page. There are also other sub-commands not used for a specific unit.

systemctl daemon-reload

When you edit a unit file already recognized by systemd, you need to reload it for systemd to recognize the changes. For example, if you edit service_a's unit file and check its status, you'll see a warning like the one below.

Command Line - INPUT
systemctl status service_a
Command Line - RESPONSE
Warning: The unit file, source configuration file or drop-ins of service_a.service changed on disk. Run 'systemctl daemon-reload' to reload units.

To make systemd recognize the change, run the command like shown below. Use sudo to avoid the authentication error if you are not logged in as the superuser.

Command Line - INPUT
sudo systemctl daemon-reload

systemctl list-dependencies

systemd manages multiple units in parallel. When the system starts, default.target is activated and dependent units are also activated. There is a tree of dependencies of the unit. By running this sub-command, you can see the dependencies in the tree structure.

Command Line - INPUT
systemctl list-dependencies
Command Line - RESPONSE
default.target
● ├─accounts-daemon.service
:
● └─multi-user.target
●   ├─apache2.service
●   ├─apport.service
●   ├─atd.service
:
●   ├─wpa_supplicant.service
●   ├─basic.target
●   │ ├─-.mount
:
●   │ ├─sockets.target
●   │ │ ├─acpid.socket
:
●   │ ├─sysinit.target
●   │ │ ├─apparmor.service
●   │ │ ├─blk-availability.service
:
●   │ └─timers.target
●   │   ├─apt-daily-upgrade.timer
:

systemctl get-default

The default target unit is used as a starting point for system boot. The default target is represented by the /etc/systemd/system/default.target file. This is a symbolic link to the original .target file in another location. To check the original target file, you can use this sub-command.

Command Line - INPUT
systemctl get-default
Command Line - RESPONSE
graphical.target

Systemctl is a command-line tool used to monitor and control systemd and units. It has several sub-commands used to show the statuses of unit files and active units or control and monitor specific units.

Commands for controlling a specific unit

There are sub-commands to control a specific unit (or unit file). If you are not logged in as the superuser, add the sudo prefix to the command.

systemctl start [unit name]

When you want to start a unit named unit_a.service, use this command.

Command Line - INPUT
sudo systemctl start unit_a

systemctl stop [unit name]

When you want to stop a running unit, use this command.

Command Line - INPUT
sudo systemctl stop unit_a

systemctl restart [unit name]

When you want to restart a running unit, use this command.

Command Line - INPUT
sudo systemctl restart unit_a

systemctl reload [unit name]

When you update a unit file and reload it, use this command.

Command Line - INPUT
sudo systemctl reload unit_a

systemctl enable [unit name]

When you want to start a unit (unit_a) automatically at boot, you need to enable it with this sub-command. When you run this command, a symbolic link of unit_a's unit file is created under a related directory of the unit that has a dependency with unit_a (typically, under a directory ending with .wants). This symbolic link is used to start unit_a.

Command Line - INPUT
sudo systemctl enable unit_a
Command Line - RESPONSE
Created symlink /etc/systemd/system/multi-user.target.wants/unit_a.service → /etc/systemd/system/unit_a.service.

With --now option, you can also start the unit when you run the command.

Command Line - INPUT
sudo systemctl enable --now unit_a

systemctl disable [unit name]

When you don't want to start the unit (unit_a) automatically at boot anymore, you can disable it with this sub-command. When you run this command, the symbolic link under the directory (ending with .wants) is removed.

Command Line - INPUT
sudo systemctl disable unit_a
Command Line - RESPONSE
Removed /etc/systemd/system/multi-user.target.wants/unit_a.service.

With --now option, you can also stop the unit when you run the command.

Command Line - INPUT
sudo systemctl disable --now unit_a

systemctl status [unit name]

When you want to check the status of a specific unit, such as ‘active(running)’ or ‘stopped’ and ‘enabled’ or ‘disabled’, you can use this sub-command. For this command, the sudo privilege is not necessary.

Command Line - INPUT
systemctl status unit_a
Command Line - RESPONSE
 unit_a.service - unit description
     Loaded: loaded (/etc/systemd/system/unit_a.service; enabled; vendor preset: >
     Active: active (running) since Sat 2023-01-14 02:45:18 UTC; 11s ago
   Main PID: 103219 (unit_a.)
      :

The status information is shown with a pager. To exit the pager mode, press the q key.

General control commands (not for a specific unit)

There are sub-commands that are not used for a specific unit, such as systemctl list-unit-files and systemctl list-unit, which are explained on the systemd topic page. There are also other sub-commands not used for a specific unit.

systemctl daemon-reload

When you edit a unit file already recognized by systemd, you need to reload it for systemd to recognize the changes. For example, if you edit service_a's unit file and check its status, you'll see a warning like the one below.

Command Line - INPUT
systemctl status service_a
Command Line - RESPONSE
Warning: The unit file, source configuration file or drop-ins of service_a.service changed on disk. Run 'systemctl daemon-reload' to reload units.

To make systemd recognize the change, run the command like shown below. Use sudo to avoid the authentication error if you are not logged in as the superuser.

Command Line - INPUT
sudo systemctl daemon-reload

systemctl list-dependencies

systemd manages multiple units in parallel. When the system starts, default.target is activated and dependent units are also activated. There is a tree of dependencies of the unit. By running this sub-command, you can see the dependencies in the tree structure.

Command Line - INPUT
systemctl list-dependencies
Command Line - RESPONSE
default.target
● ├─accounts-daemon.service
:
● └─multi-user.target
●   ├─apache2.service
●   ├─apport.service
●   ├─atd.service
:
●   ├─wpa_supplicant.service
●   ├─basic.target
●   │ ├─-.mount
:
●   │ ├─sockets.target
●   │ │ ├─acpid.socket
:
●   │ ├─sysinit.target
●   │ │ ├─apparmor.service
●   │ │ ├─blk-availability.service
:
●   │ └─timers.target
●   │   ├─apt-daily-upgrade.timer
:

systemctl get-default

The default target unit is used as a starting point for system boot. The default target is represented by the /etc/systemd/system/default.target file. This is a symbolic link to the original .target file in another location. To check the original target file, you can use this sub-command.

Command Line - INPUT
systemctl get-default
Command Line - RESPONSE
graphical.target
Tag: