Home/Server & Website Managed Operations/Systemd Service Management Guide
Server & Website Managed Operations

Systemd Service Management Guide

Both Alibaba Cloud and AWS have released CentOS 7 images. When purchasing cloud servers and configuring the system, you now have another option.

However, CentOS 7 has adopted systemd as the default init system. With the adoption of systemd, the systemctl command is used instead of the traditional service command. Therefore, studying and learning about systemd services has become more urgent.

This article uses an Alibaba Cloud server running CentOS 7 as an example to explain systemd service management.

1. Systemd Service Management

To date, systemd defines service startup scripts in the form of Units. Managing services is equivalent to managing Units.

2. List of Active Units (list-units)

The default option for the systemctl command is systemctl list-units. Therefore, even without adding options, the results of list-units will be displayed.

Additionally, if you add the --no-pager option to the systemctl command, the entire result page will be displayed.

# systemctl list-units
UNIT                        LOAD   ACTIVE SUB       DESCRIPTION
proc-sys...t_misc.automount loaded active running   Arbitrary Executable File Fo
sys-devi...tty-ttyS0.device loaded active plugged   /sys/devices/platform/serial
sys-devi...tty-ttyS1.device loaded active plugged   /sys/devices/platform/serial
sys-devi...tty-ttyS2.device loaded active plugged   /sys/devices/platform/serial
sys-devi...tty-ttyS3.device loaded active plugged   /sys/devices/platform/serial
sys-devi...vdb-xvdb1.device loaded active plugged   /sys/devices/vbd-51728/block
sys-devi...lock-xvdb.device loaded active plugged   /sys/devices/vbd-51728/block
sys-devi...vda-xvda1.device loaded active plugged   /sys/devices/vbd-768/block/x
sys-devi...lock-xvda.device loaded active plugged   /sys/devices/vbd-768/block/x
sys-devi...-net-eth0.device loaded active plugged   /sys/devices/vif-0/net/eth0
sys-devi...-net-eth1.device loaded active plugged   /sys/devices/vif-1/net/eth1
sys-module-configfs.device  loaded active plugged   /sys/module/configfs
(省略)
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.

88 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.

3. List of Installed Unit Files (list-unit-files)

[root@iZ2516ti6utZ ~]# systemctl list-unit-files
UNIT FILE                              STATE
proc-sys-fs-binfmt_misc.automount      static
dev-hugepages.mount                    static
dev-mqueue.mount                       static
proc-sys-fs-binfmt_misc.mount          static
sys-fs-fuse-connections.mount          static
sys-kernel-config.mount                static
sys-kernel-debug.mount                 static
tmp.mount                              disabled
brandbot.path                          disabled
cups.path                              disabled
(省略)
239 unit files listed.

For example, if you install Apache using yum install httpd, the following Unit file will be recorded.

# systemctl list-unit-files | grep httpd
httpd.service                          disabled

4. Enabling a Unit (enable)

When enabled, it will appear as a startup service when the system boots. systemctl enable httpd is equivalent to chkconfig httpd on.

# systemctl enable httpd
ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multi-user.target.wants/httpd.service'

Additionally, multi-user is equivalent to runlevel 2, 3, or 4, with the following functions:

runlevel0 -> poweroff
runlevel1 -> rescue
runlevel2 -> multi-user
runlevel3 -> multi-user
runlevel4 -> multi-user
runlevel5 -> graphical
runlevel6 -> reboot

After enabling, previously disabled services will become enabled.

# systemctl list-unit-files --no-pager | grep httpd
httpd.service                          enabled

5. Disabling a Unit (disable)

After disabling, it will be removed from the list of startup services when the system boots. systemctl disable httpd is equivalent to chkconfig httpd off.

# systemctl disable httpd
rm '/etc/systemd/system/multi-user.target.wants/httpd.service'

After disabling, you can verify it with list-unit-files, which will show it as disabled.

# systemctl list-unit-files | grep httpd
httpd.service                          disabled

6. Checking Unit Enable/Disable Status (is-enabled)

If enabled, it returns enabled; if disabled, it returns disabled.

# systemctl is-enabled httpd
disabled
# systemctl enable httpd
ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multi-user.target.wants/httpd.service' 
# systemctl is-enabled httpd
enabled

7. Re-enabling a Unit (reenable)

If re-enabled, it changes from disabled to enabled, and the symbolic link for the startup script is recreated.

# systemctl reenable httpd
rm '/etc/systemd/system/multi-user.target.wants/httpd.service'
ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multi-user.target.wants/httpd.service'

8. Starting a Unit (start)

With the service command, the startup process is displayed. However, with systemctl, no specific information is shown even after starting.

# systemctl start httpd
# ps aux | grep httpd
root     28023  0.0  0.4 209696  4784 ?        Ss   15:19   0:00 /usr/sbin/httpd -DFOREGROUND
apache   28024  0.0  0.2 211780  2964 ?        S    15:19   0:00 /usr/sbin/httpd -DFOREGROUND
apache   28025  0.0  0.2 211780  2964 ?        S    15:19   0:00 /usr/sbin/httpd -DFOREGROUND
apache   28026  0.0  0.2 211780  2964 ?        S    15:19   0:00 /usr/sbin/httpd -DFOREGROUND
apache   28027  0.0  0.2 211780  2964 ?        S    15:19   0:00 /usr/sbin/httpd -DFOREGROUND
apache   28028  0.0  0.2 211780  2964 ?        S    15:19   0:00 /usr/sbin/httpd -DFOREGROUND
root     28035  0.0  0.0 112640   964 pts/0    S+   15:20   0:00 grep --color=auto httpd

9. Checking Unit Startup Status (status)

 

10. Viewing Detailed Unit Information (show)

To check detailed Unit information, viewing it directly via status is better, but for script processing, show is preferable.

# systemctl show httpd
Id=httpd.service
Names=httpd.service
Requires=basic.target
Wants=system.slice
Conflicts=shutdown.target
Before=shutdown.target
After=network.target remote-fs.target nss-lookup.target systemd-journald.socket basic.target system.slice
Description=The Apache HTTP Server
LoadState=loaded
ActiveState=active
SubState=running
……
……

11. Stopping a Unit (stop)

Stopping a Unit is similar; no specific information is displayed.

# systemctl stop httpd
# ps aux | grep httpd
root     28054  0.0  0.0 112640   964 pts/0    S+   15:29   0:00 grep --color=auto httpd

12. Reloading Unit Configuration Files (reload)

Whether it can be reloaded depends on the Unit.

# systemctl reload httpd

13. Restarting a Unit (restart)

Like start and stop, it does not display information.

# systemctl restart httpd

13-1. Attempting to Restart a Unit (try-restart)

If the Unit is running, it restarts; if stopped, no action is taken.

# systemctl try-restart httpd

14. Reloading or Restarting a Unit (reload-or-restart)

If the Unit can be reloaded, it performs a reload; otherwise, it performs a restart. If the Unit is stopped, it starts it.

# systemctl reload-or-restart httpd

Or if the Unit is stopped:

# systemctl stop httpd
# systemctl reload-or-restart httpd
# systemctl status httpd
httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled)
   Active: active (running) since Mon 2015-03-23 15:44:18 CST; 19s ago
  Process: 28113 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
  Process: 28081 ExecReload=/usr/sbin/httpd $OPTIONS -k graceful (code=exited, status=0/SUCCESS)
 Main PID: 28121 (httpd)
……
……

14-1. Attempting to Reload or Restart a Unit (reload-or-try-restart)

Similar to reload-or-restart, but it does not start the Unit if it is stopped.

# systemctl reload-or-try-restart httpd
# systemctl stop httpd
# systemctl reload-or-try-restart httpd
Job for httpd.service failed. See 'systemctl status httpd.service' and 'journalc

15. Forcefully Killing a Unit (kill)

# systemctl kill httpd

16. Masking/Unmasking a Unit (mask/unmask)

Even if a Unit is disabled (disable), the service might still start. Masking prevents the service from starting itself.

# systemctl mask httpd
ln -s '/dev/null' '/etc/systemd/system/httpd.service'
# systemctl start httpd
Failed to issue method call: Unit httpd.service is masked.

In the masked state, is-enabled returns masked.

# systemctl is-enabled httpd
masked

To unmask, execute unmask.

# systemctl unmask httpd
rm '/etc/systemd/system/httpd.service'

17. Checking Unit Startup Status (is-active)

If is-active is used, it returns active if the unit is running.
# systemctl start httpd
# systemctl is-active httpd
active

If it is not started, it returns unknown.

# systemctl stop httpd
# systemctl is-active httpd
Unknown

18. Checking Unit Failure Status (is-failed)

is-failed returns active if the unit started normally.
# systemctl is-failed httpd
Active
If the Unit cannot start normally and encounters an error, it displays failed.
# mv /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.org
# systemctl restart httpd
Job for httpd.service failed. See 'systemctl status httpd.service' and 'journalctl -xn' for details.
# systemctl is-failed httpd
failed

19. Resetting Unit Failure Status.

Reset the failure status recognized by systemd.

# systemctl is-failed httpd
failed
# systemctl reset-failed httpd
# systemctl is-failed httpd
unknown

20. Checking Unit Dependencies (list-dependencies)

A list of Units that this Unit depends on.

Systemd is unique and quite interesting, but mastering it takes considerable effort and time. It is foreseeable that systemd will eventually become the mainstream. We hope this article helps you in your study of systemd.
FREE CHECKUP

Free server checkup

We will send a problem checklist and remediation advice within one business day — no obligation to engage.

135 410 16684
Weekdays Mon – Fri 10:00 – 22:00 (UTC+8) · 7×24 emergency response
Call now
CONTACT

Contact Us

How can we help?

* Required