+135 410 16684Mon. - Fri. 10:00-22:00

Systemd服务管理说明

服务器运维工程师。AWS,阿里云等服务器配置&故障排查的技术支持。

Systemd服务管理说明

阿里云和AWS,都公布了CentOS 7镜像。购买云主机,配置系统时,又多了一个选择。
然而,CentOS 7,已将systemd作为默认的init系统,
随着Systemd的采用,使用systemctl命令而非原来的service命令。因此,对systemd服务的学习研究显得更为迫切了。
本文以阿里云服务器,Centos7系统为例。进行讲解说明Systemd服务的管理。

1.Systemd服务管理

Systemd到目前为止,服务启动脚本是通过Unit形式来定义的。服务的管理也就等于Unit的管理。

2.有效的Unit一览表(list-units)

Systemctl命令默认的选项为【systemctl list-units】因此,即使不添加选项,list-units的结果也会显示出来。
另外,Systemctl命令,添加–no-pager选项的话,结果页面会全部显示出来。

# 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.安装的Unit文件一览表(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.

例如,用yum install httpd安装Apache的话,会记载如下所示的Unit文件。

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

4.Unit的有效化(enable)

有效的话,如下,系统启动时,会作为启动服务显示出来。systemctl enable httpd和chkonfig httpd on是一样的。

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

另外,所谓的multi-user和runlevel 2 or 3 or 4相同,作用如下。

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

有效的话,安装的后为disable的服务会变成enable。

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

5.Unit无效化 (disable)

无效之后,如下,系统启动时,将从启动的服务对象中删除。systemctl disable httpd和chkonfig httpd off是一样的。

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

无效化之后,可用list-unit-files确认,已为disable。

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

6.Unit有效/无效的确认(is-enable)

有效的话为enabled,无效的话为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.Unit的再有效化 (reenable)

若再有效化,会由disable变为enable,启动脚本的软链接会执行重新粘贴动作。

# 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.Unit的启动 (start)

如果是service命令的话,会显示启动经过。但是systemctl的话即使启动,也不会特意显示信息出来。

# 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.Unit启动状态的确 (status)

 

10.Unit详细信息确认(show)

确认Unit的详细信息,仅仅用眼查看,还是status为好,但是脚本处理的话,show倒会更好。

# 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.Unit的停止(stop)

Unit的停止也是一样的,不会特意显示信息。

# 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.重装Unit的配置文件(reload)

能不能重新加载,是由Unit决定的。

# systemctl reload httpd

13.Unit的重起(restart)

和start,stop一样,不会显示信息。

# systemctl restart httpd

13-1.试行Unit的重起(try-restart)
Unit启动中的话,进行重启,Unit停止中的话,不进行任何操作。

# systemctl try-restart httpd

14.Unit重装或重起(reload-or -restart)

Unit如果可以重装的话,就reload,不能重装的话就执行restart。或者Unit停止的话进行启动。

# systemctl reload-or-restart httpd

或Unit停止

# 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.试行Unit重装或重起(reload-or-try-restart)
和reload-or-restart一样,但是Unit如果停止中的话不启动。

# 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.Unit的強制关闭(kill)

# systemctl kill httpd

16.Unit的屏蔽(mask/unmask)

即使Unit无效(disable)的话,服务仍有可能启动。mask的话服务自身就不能启动了。

# 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.

屏蔽的情况下,is-enabled结果会返回masked。

# systemctl is-enabled httpd
masked

解除屏蔽的话,执行unmask。

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

17.确认Unit的启动状态 (is-active)

is-active的话,若unit启动中的话,就会返回active。
# systemctl start httpd
# systemctl is-active httpd
active

如果没有启动,就会返回unknown。

# systemctl stop httpd
# systemctl is-active httpd
Unknown

18.确认Unit异常状况(is-failed)

is-failed在unit正常启动的话会返回active。
# systemctl is-failed httpd
Active
Unit不能正常启动,出现异常的话,显示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.Unit异常状态的重启。

重启识别的systemd异常状态。

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

20.Unit依赖关系的确认(list-dependencies)

该unit所依赖的unit一览表。

Systemd是别具特色且相当有趣的,但是掌握起来还是要花费不少功夫和时间。可以预见,Systemd终将会成为主流。愿本文对大家在Systemd的学习方面有所帮助。