10.2. Managing System Services
Note
/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. In Red Hat Enterprise Linux 7, these init scripts have been replaced with service units.
.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 10.3, “Comparison of the service Utility with systemctl ”, Table 10.4, “Comparison of the chkconfig Utility with systemctl”, and further in this section. The service
and chkconfig
commands are still available in the system and work as expected, but are only included for compatibility reasons and should be avoided.
Table 10.3. Comparison of the service Utility with systemctl
service | systemctl | Description |
---|---|---|
service name start
| systemctl start name.service
| Starts a service. |
service name stop
| systemctl stop name.service
| Stops a service. |
service name restart
| systemctl restart name.service
| Restarts a service. |
service name condrestart
| systemctl try-restart name.service
| Restarts a service only if it is running. |
service name reload
| systemctl reload name.service
| Reloads configuration. |
service name status
| systemctl status name.service
systemctl is-active name.service
| Checks if a service is running. |
service --status-all
| systemctl list-units --type service --all
| Displays the status of all services. |
Table 10.4. Comparison of the chkconfig Utility with systemctl
chkconfig | systemctl | Description |
---|---|---|
chkconfig name on
| systemctl enable name.service
| Enables a service. |
chkconfig name off
| systemctl disable name.service
| Disables a service. |
chkconfig --list name
| systemctl status name.service
systemctl is-enabled name.service
| Checks if a service is enabled. |
chkconfig --list
| systemctl list-unit-files --type service
| Lists all services and checks if they are enabled. |
chkconfig --list
| systemctl list-dependencies --after
| Lists services that are ordered to start before the specified unit. |
chkconfig --list
| systemctl list-dependencies --before
| Lists services that are ordered to start after the specified unit. |
Specifying Service Units
.service
file extension, for example:
~]# systemctl stop nfs-server.service
systemctl
utility assumes the argument is a service unit. The following command is equivalent to the one above:
~]# systemctl stop nfs-server
~]# systemctl show nfs-server.service -p Names
Behavior of systemctl
in a chroot
Environment
chroot
command, most systemctl
commands refuse to perform any action. The reason for this is that the systemd
process and the user that used the chroot
command do not have the same view of the filesystem. This happens, for example, when systemctl
is invoked from a kickstart
file.
systemctl enable
and systemctl disable
commands. These commands do not need a running system and do not affect running processes, but they do affect unit files. Therefore, you can run these commands even in chroot
environment. For example, to enable the httpd
service on a system under the /srv/website1/
directory:
~]#chroot /srv/website1
~]#systemctl enable httpd.service
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service, pointing to /usr/lib/systemd/system/httpd.service.
10.2.1. Listing Services
systemctl list-units --type service
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
).
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
systemctl list-unit-files --type service
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 Section 10.2.2, “Displaying Service Status”.
Example 10.1. Listing Services
~]$ systemctl list-units --type service
UNIT LOAD ACTIVE SUB DESCRIPTION
abrt-ccpp.service loaded active exited Install ABRT coredump hook
abrt-oops.service loaded active running ABRT kernel log watcher
abrt-vmcore.service loaded active exited Harvest vmcores for ABRT
abrt-xorg.service loaded active running ABRT Xorg log watcher
abrtd.service loaded active running ABRT Automated Bug Reporting Tool
...
systemd-vconsole-setup.service loaded active exited Setup Virtual Console
tog-pegasus.service loaded active running OpenPegasus CIM Server
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.
46 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'
~]$ systemctl list-unit-files --type service
UNIT FILE STATE
abrt-ccpp.service enabled
abrt-oops.service enabled
abrt-vmcore.service enabled
abrt-xorg.service enabled
abrtd.service enabled
...
wpa_supplicant.service disabled
ypbind.service disabled
208 unit files listed.
10.2.2. Displaying Service Status
systemctl status name.service
gdm
). This command displays the name of the selected service unit followed by its short description, one or more fields described in Table 10.5, “Available Service Unit Information”, and if it is executed by the root
user, also the most recent log entries.
Table 10.5. 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). |
systemctl is-active name.service
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 Section 10.2.1, “Listing Services”.
Example 10.2. Displaying Service Status
gdm.service
. To determine the current status of this service unit, type the following at a shell prompt:
~]# systemctl status gdm.service
gdm.service - GNOME Display Manager
Loaded: loaded (/usr/lib/systemd/system/gdm.service; enabled)
Active: active (running) since Thu 2013-10-17 17:31:23 CEST; 5min ago
Main PID: 1029 (gdm)
CGroup: /system.slice/gdm.service
├─1029 /usr/sbin/gdm
├─1037 /usr/libexec/gdm-simple-slave --display-id /org/gno...
└─1047 /usr/bin/Xorg :0 -background none -verbose -auth /r...
Oct 17 17:31:23 localhost systemd[1]: Started GNOME Display Manager.
Example 10.3. Displaying Services Ordered to Start Before a Service
~]# systemctl list-dependencies --after gdm.service
gdm.service
├─dbus.socket
├─getty@tty1.service
├─livesys.service
├─plymouth-quit.service
├─system.slice
├─systemd-journald.socket
├─systemd-user-sessions.service
└─basic.target
[output truncated]
Example 10.4. Displaying Services Ordered to Start After a Service
~]# systemctl list-dependencies --before gdm.service
gdm.service
├─dracut-shutdown.service
├─graphical.target
│ ├─systemd-readahead-done.service
│ ├─systemd-readahead-done.timer
│ └─systemd-update-utmp-runlevel.service
└─shutdown.target
├─systemd-reboot.service
└─final.target
└─systemd-reboot.service
10.2.3. Starting a Service
root
:
systemctl start name.service
gdm
). This command starts the selected service unit in the current session. For information on how to enable a service unit to be started at boot time, see Section 10.2.6, “Enabling a Service”. For information on how to determine the status of a certain service unit, see Section 10.2.2, “Displaying Service Status”.
Example 10.5. Starting a Service
httpd.service
. To activate this service unit and start the httpd
daemon in the current session, run the following command as root
:
~]# systemctl start httpd.service
10.2.4. Stopping a Service
root
:
systemctl stop name.service
bluetooth
). This command stops the selected service unit in the current session. For information on how to disable a service unit and prevent it from being started at boot time, see Section 10.2.7, “Disabling a Service”. For information on how to determine the status of a certain service unit, see Section 10.2.2, “Displaying Service Status”.
Example 10.6. Stopping a Service
bluetoothd
daemon is named bluetooth.service
. To deactivate this service unit and stop the bluetoothd
daemon in the current session, run the following command as root
:
~]# systemctl stop bluetooth.service
10.2.5. Restarting a Service
root
:
systemctl restart name.service
httpd
). 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
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 10.2.2, “Displaying Service Status”.
Example 10.7. Restarting a Service
root
:
~]# systemctl reload httpd.service
10.2.6. Enabling a Service
root
:
systemctl enable name.service
httpd
). 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. This command does not, however, rewrite links that already exist. If you want to ensure that the symbolic links are re-created, use the following command as root
:
systemctl reenable name.service
Example 10.8. Enabling a Service
root
:
~]# systemctl enable httpd.service
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
10.2.7. Disabling a Service
root
:
systemctl disable name.service
bluetooth
). 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
/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
Example 10.9. Disabling a Service
bluetooth.service
unit in the current session. To prevent this service unit from starting at boot time, type the following at a shell prompt as root
:
~]# systemctl disable bluetooth.service
Removed symlink /etc/systemd/system/bluetooth.target.wants/bluetooth.service.
Removed symlink /etc/systemd/system/dbus-org.bluez.service.
10.2.8. Starting a Conflicting Service
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.