SystemD - Managing System Services (part 2)
Previous versions of Linux distributions like Debian or Ubuntu, which were distributed with SysV init or Upstart, used init scripts located in the /etc/rc.d/init.d/
directory. These init scripts were typically written in Bash, and allowed the system administrator to control the state of services and daemons in their system. With systemd these init scripts have been replaced with service units.
Service units end with the .service
file extension and serve a similar purpose as init scripts. To view, start, stop, restart, enable, or disable
system services, use the systemctl
command as described in Table 2.1 - systemctl utility
Table 2.1 - systemctl utility
systemctl | descritption |
---|---|
systemctl start name.service |
Starts a service. |
systemctl stop name.service |
Stop a service |
systemctl restart name.service |
Restart a service |
systemctl try-restart name.service |
Restarts a service only if it is running. |
systemctl reload name.service |
Reloads configuration. |
systemctl status name.service |
Checks if a service is running. |
systemctl list-units --type service --all |
Displays the status of all services. |
systemctl | descritption |
---|---|
systemctl enable name.service |
Enables a service |
systemctl disable name.service |
Disables a service |
systemctl status name.service |
Checks if a service is enabled. |
systemctl is-enabled name.service |
Checks if a service is enabled. |
systemctl list-unit-files --type service |
Lists all services and checks if they are enabled. |
systemctl list-dependencies --after |
Lists services that are ordered to start before the specified unit. |
systemctl list-dependencies --before |
Lists services that are ordered to start after the specified unit. |
2.1 Specifying Service Units
For clarity, all command examples in the rest of this section use full unit names with the .service
file extension, for example:
~]$ systemctl stop apache2.service
However, the file extension can be omitted, in which case the systemctl utility assumes the argument is a service unit. The following command is equivalent to the one above:
~]$ systemctl stop apache2
Additionally, some units have alias names. Those names can have shorter names than units, which can be used instead of the actual unit names. To find all aliases that can be used for a particular unit, use:
~]$ systemctl show apache2.service -p Names
2.2 Listing Services
To list all currently loaded service units, type the following at a shell prompt:
root@hostname:~$ systemctl list-units --type service
UNIT LOAD ACTIVE SUB DESCRIPTION
-----------------------------------------------------------------------------------------------------
apache2.service loaded active running The Apache HTTP Server
console-setup.service loaded active exited Set console font and keymap
cron.service loaded active running Regular background program processing daemon
dbus.service loaded active running D-Bus System Message Bus
.
.
.
For each service unit file, this command displays its full name (UNIT)
followed by a note whether the unit file has been loaded (LOAD)
, its high-level (ACTIVE)
and low-level (SUB)
unit file activation state, and a short description (DESCRIPTION)
.
By default, the systemctl list-units
command displays only active units. If you want to list all loaded units regardless of their state, run this command with the --all
or -a
command line option:
~]$ systemctl list-units --type service --all
UNIT LOAD ACTIVE SUB DESCRIPTION
--------------------------------------------------------------------------------------------------------
apache2.service loaded active running The Apache HTTP Server
● apparmor.service not-found inactive dead apparmor.service
apt-daily-upgrade.service loaded inactive dead Daily apt upgrade and clean activities
apt-daily.service loaded inactive dead Daily apt download activities
● auditd.service not-found inactive dead auditd.service
.
.
.
You can also list all available service units to see if they are enabled. To do so, type:
~]$ systemctl list-unit-files --type service
UNIT FILE STATE
-----------------------------------------------
apache-htcacheclean.service disabled
apache-htcacheclean@.service disabled
apache2.service enabled
apache2@.service disabled
apt-daily-upgrade.service static
apt-daily.service static
autovt@.service enabled
bootlogd.service masked
.
.
.
For each service unit, this command displays its full name (UNIT FILE)
followed by information whether the service unit is enabled or not (STATE)
. For information on how to determine the status of individual service units, see Table 2, "Available Service Unit Information"
2.3 Displaying Service Status
To display detailed information about a service unit that corresponds to a system service, type the following at a shell prompt:
~]$ systemctl status name.service
Replace name
with the name of the service unit you want to inspect (for example, apache2). This command displays the name of the selected service unit followed by its short description, one or more fields described in Table 2, "Available Service Unit Information"
, and if it is executed by the root user, also the most recent log entries.
Table 2 - Available Service Unit Information
Field | Description |
---|---|
Loaded |
Information whether the service unit has been loaded, the absolute path to the unit file, and a note whether the unit is enabled. |
Active |
Information whether the service unit is running followed by a time stamp. |
Main Pid |
The PID of the corresponding system service followed by its name. |
Status |
Additional information about the corresponding system service. |
Process |
Additional information about related processes. |
CGroup |
Additional information about related Control Groups (cgroups). |
To only verify that a particular service unit is running, run the following command:
~]$ systemctl is-active name.service
Similarly, to determine whether a particular service unit is enabled, type:
~]$ systemctl is-enabled name.service
systemctl is-active
and systemctl is-enabled
return an exit status of 0 if the specified service unit is running or enabled. For information on how to list all currently loaded service units, see 2.2 Listing Services
Example Displaying Service Status
The service unit for the apache2 web server is named apache2.service. To determine the current status of this service unit, type the following at a shell prompt:
~]$ systemctl status apache2.service
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2018-01-31 14:48:36 CET; 1h 22min ago
Process: 11914 ExecStop=/usr/sbin/apachectl stop (code=exited, status=0/SUCCESS)
Process: 10215 ExecReload=/usr/sbin/apachectl graceful (code=exited, status=0/SUCCESS)
Process: 11922 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
Main PID: 11926 (apache2)
Tasks: 11 (limit: 4915)
CGroup: /system.slice/apache2.service
├─11926 /usr/sbin/apache2 -k start
├─11946 /usr/sbin/apache2 -k start
├─12761 /usr/sbin/apache2 -k start
├─13010 /usr/sbin/apache2 -k start
├─13362 /usr/sbin/apache2 -k start
├─13393 /usr/sbin/apache2 -k start
├─13481 /usr/sbin/apache2 -k start
├─13509 /usr/sbin/apache2 -k start
├─13510 /usr/sbin/apache2 -k start
├─13527 /usr/sbin/apache2 -k start
└─13528 /usr/sbin/apache2 -k start
Jan 31 14:48:35 websystem-test systemd[1]: Stopped The Apache HTTP Server.
Jan 31 14:48:35 websystem-test systemd[1]: Starting The Apache HTTP Server...
Jan 31 14:48:36 websystem-test systemd[1]: Started The Apache HTTP Server.
Example Displaying Services Ordered to Start Before a Service
To determine what services are ordered to start before the specified service, type the following at a shell prompt:
~]$ systemctl list-dependencies --after apache2.service
apache2.service
● ├─-.mount
● ├─system.slice
● ├─systemd-journald.socket
● ├─basic.target
● │ ├─-.mount
● │ ├─tmp.mount
● │ ├─paths.target
● │ │ ├─systemd-ask-password-console.path
● │ │ └─systemd-ask-password-wall.path
● │ ├─slices.target
● │ │ ├─-.slice
● │ │ ├─system.slice
● │ │ └─user.slice
● │ ├─sockets.target
● │ │ ├─dbus.socket
● │ │ ├─syslog.socket
● │ │ ├─systemd-initctl.socket
.
.
. And more
Example Displaying Services Ordered to Start After a Service
To determine what services are ordered to start after the specified service, type the following at a shell prompt:
~]$ systemctl list-dependencies --before apache2.service
apache2.service
● ├─multi-user.target
● │ ├─systemd-update-utmp-runlevel.service
● │ └─graphical.target
● │ └─systemd-update-utmp-runlevel.service
● └─shutdown.target
2.4 Starting a Service
To start a service unit that corresponds to a system service, type the following at a shell prompt as root
:
~]$ systemctl start name.service
Replace name
with the name of the service unit you want to start (for example, apache2).
2.5 Stopping a Service
To stop a service unit that corresponds to a system service, type the following at a shell prompt as root
:
~]$ systemctl stop name.service
Replace name
with the name of the service unit you want to stop (for example, apache2).
2.6 Restarting a Service
To restart a service unit that corresponds to a system service, type the following at a shell prompt as root
:
~]$ systemctl restart name.service
Replace name
with the name of the service unit you want to stop (for example, apache2).
This command stops the selected service unit in the current session and immediately starts it again.
Importantly, if the selected service unit is not running, this command starts it too. To tell systemd to restart a service unit only if the corresponding service is already running, run the following command as root:
~]$ systemctl try-restart name.service
Certain system services also allow you to reload their configuration without interrupting their execution. To do so, type as root:
~]$ systemctl reload name.service
systemctl
command also supports the reload-or-restart
and reload-or-try-restart
commands that restart such services instead. For information on how to determine the status of a certain service unit, see Section Displaying Service Status
.
2.7 Enabling a service
To configure a service unit that corresponds to a system service to be automatically started at boot time, type the following at a shell prompt as root
:
~]$ systemctl enable name.service
Replace name
with the name of the service unit you want to enable (for example, apache2). This command reads the [Install]
section of the selected service unit and creates appropriate symbolic links to the /usr/lib/systemd/system/name.service
file in the /etc/systemd/system/
directory and its subdirectories.
root
: systemctl reenable name.service
This command disables the selected service unit and immediately enables it again. For information on how to determine whether a certain service unit is enabled to start at boot time, see Section Displaying Service Status .
2.8. Disabling a Service
To prevent a service unit that corresponds to a system service from being automatically started at boot time, type the following at a shell prompt as root
:
~]$ systemctl disable name.service
Replace name
with the name of the service unit you want to disable (for example, postfix). This command reads the [Install]
section of the selected service unit and removes appropriate symbolic links to the /usr/lib/systemd/system/name.service
file from the /etc/systemd/system/
directory and its subdirectories. In addition, you can mask
any service unit to prevent it from being started manually or by another service. To do so, run the following command as root
:
~]$ systemctl mask name.service
This command replaces the /etc/systemd/system/name.service
file with a symbolic link to /dev/null
, rendering the actual unit file inaccessible to systemd. To revert this action and unmask a service unit, type as root
:
~]$ systemctl unmask name.service
systemctl mask name.service
. To revert this action and unmask a service unit, type as root systemctl unmask name.service
2.9 Starting a Conflicting Service
In systemd
, positive and negative dependencies between services exist. Starting particular service may require starting one or more other services (positive dependency)
or stopping one or more services (negative dependency)
.
When you attempt to start a new service, systemd
resolves all dependencies automatically. Note that this is done without explicit notification to the user.
For example, if you are running the postfix
service, and you try to start the sendmail
service, systemd first automatically stops postfix
, because these two services are conflicting and cannot run on the same port.
Another parts of this guide: