10.2. Managing System Services
/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.
.servicefile extension and serve a similar purpose as init scripts. To view, start, stop, restart, enable, or disable system services, use the
systemctlcommand 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
chkconfigcommands 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
| || ||Starts a service.|
| || ||Stops a service.|
| || ||Restarts a service.|
| || ||Restarts a service only if it is running.|
| || ||Reloads configuration.|
| || ||Checks if a service is running.|
| || ||Displays the status of all services.|
Table 10.4. Comparison of the chkconfig Utility with systemctl
| || ||Enables a service.|
| || ||Disables a service.|
| || ||Checks if a service is enabled.|
| || ||Lists all services and checks if they are enabled.|
| || ||Lists services that are ordered to start before the specified unit.|
| || ||Lists services that are ordered to start after the specified unit.|
Specifying Service Units
.servicefile extension, for example:
systemctl stop nfs-server.service
systemctlutility 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
systemctl in a
systemctlcommands refuse to perform any action. The reason for this is that the
systemdprocess and the user that used the
chrootcommand do not have the same view of the filesystem. This happens, for example, when
systemctlis invoked from a
systemctl disablecommands. 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
chrootenvironment. For example, to enable the
httpdservice on a system under the
systemctl enable httpd.serviceCreated 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 (
systemctl list-unitscommand displays only active units. If you want to list all loaded units regardless of their state, run this command with the
-acommand 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 serviceUNIT 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 serviceUNIT 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
rootuser, also the most recent log entries.
Table 10.5. Available Service Unit Information
| ||Information whether the service unit has been loaded, the absolute path to the unit file, and a note whether the unit is enabled.|
| ||Information whether the service unit is running followed by a time stamp.|
| ||The PID of the corresponding system service followed by its name.|
| ||Additional information about the corresponding system service.|
| ||Additional information about related processes.|
| ||Additional information about related Control Groups (cgroups).|
systemctl is-active name.service
systemctl is-enabled name.service
systemctl is-enabledreturn an exit status of
0if 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.servicegdm.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: Started GNOME Display Manager.
Example 10.3. Displaying Services Ordered to Start Before a Service
systemctl list-dependencies --after gdm.servicegdm.service ├─dbus.socket ├─email@example.com ├─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.servicegdm.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
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
httpddaemon in the current session, run the following command as
systemctl start httpd.service
10.2.4. Stopping a Service
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
bluetoothddaemon is named
bluetooth.service. To deactivate this service unit and stop the
bluetoothddaemon in the current session, run the following command as
systemctl stop bluetooth.service
10.2.5. Restarting a Service
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
systemctl try-restart name.service
systemctl reload name.service
systemctlcommand also supports the
reload-or-try-restartcommands 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
systemctl reload httpd.service
10.2.6. Enabling a Service
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.servicefile 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
systemctl reenable name.service
Example 10.8. Enabling a Service
systemctl enable httpd.serviceCreated symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
10.2.7. Disabling a Service
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.servicefile 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
systemctl mask name.service
/etc/systemd/system/name.servicefile 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
systemctl unmask name.service
Example 10.9. Disabling a Service
bluetooth.serviceunit in the current session. To prevent this service unit from starting at boot time, type the following at a shell prompt as
systemctl disable bluetooth.serviceRemoved 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
postfixservice, and you try to start the
sendmailservice, systemd first automatically stops
postfix, because these two services are conflicting and cannot run on the same port.