systemd服务管理

在systemd中,之前服务的启动脚本将以Unit(单元)的形式存在,因此服务管理=单元管理。

显示所有已启用的Unit(list-units)

因为systemctl命令的默认选项是systemctl list-units,不添加任何选项的话,将显示list-units的结果。

另外在systemctl命令中加上--no-pager选项后将不翻页显示所有单元的内容。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ systemctl list-units
UNIT                                             LOAD   ACTIVE SUB       DESCRIPTION
proc-sys-fs-binfmt_misc.automount                loaded active waiting   Arbitrary Executable File Formats File System Automount Point
sys-devices-platform-serial8250-tty-ttyS1.device loaded active plugged   /sys/devices/platform/serial8250/tty/ttyS1
sys-devices-platform-serial8250-tty-ttyS2.device loaded active plugged   /sys/devices/platform/serial8250/tty/ttyS2
sys-devices-platform-serial8250-tty-ttyS3.device loaded active plugged   /sys/devices/platform/serial8250/tty/ttyS3
sys-devices-pnp0-00:09-tty-ttyS0.device          loaded active plugged   /sys/devices/pnp0/00:09/tty/ttyS0
(--snip--)
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.
96 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.

统一显示已安装的Unit文件(list-unit-files)

1
2
3
4
5
6
7
8
9
10
11
12
$ systemctl list-unit-files
UNIT FILE                              STATE
proc-sys-fs-binfmt_misc.automount      static
dev-hugepages.mount                    static
sys-kernel-debug.mount                 static
tmp.mount                              masked
var-lib-nfs-rpc_pipefs.mount           static
brandbot.path                          disabled
arp-ethers.service                     disabled
auditd.service                         enabled
(--snip--)
207 unit files listed.

比如执行了yum install httpd命令安装Apache后,会追加这样一个Unit文件。

1
2
$ systemctl list-unit-files | grep httpd
httpd.service                          disabled

启用Unit(enable)

启用Unit后,会如下所示系统在开机时就会启动服务。systemctl enable httpd命令与之前CentOS6中chkonfig httpd on的作用是一样的。

1
2
$ sudo systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.

另外multi-user即相当于runlevel(init) 2 or 3 or 4。各运行状态如下所示。

  • runlevel0 -> poweroff 关机
  • runlevel1 -> rescue 单用户模式(救援模式)
  • runlevel2 -> multi-user 多用户模式(无网络、字符界面)
  • runlevel3 -> multi-user 多用户模式(有网络、字符界面)
  • runlevel4 -> multi-user 备用模式(现已基本废弃)
  • runlevel5 -> graphical 图形界面
  • runlevel6 -> reboot 重启系统

命令执行后,原本安装后默认禁用(disable)的服务将被启用。

1
2
$ systemctl list-unit-files --no-pager | grep httpd
httpd.service                          enabled

禁用Unit(disable)

禁用后,该服务(启动项)将被删除。systemctl disable httpdchkonfig httpd off作用等同。

1
2
$ sudo systemctl disable httpd
Removed symlink /etc/systemd/system/multi-user.target.wants/httpd.service.

使用list-unit-files命令确认是否被禁用。

1
2
$ systemctl list-unit-files --no-pager | grep httpd
httpd.service                          disabled

查看Unit是否启用/禁用(is-enable)

启用时显示enabled,禁用时显示disabled。

1
2
3
4
5
6
$ sudo systemctl is-enabled httpd
disabled
$ sudo systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
$ sudo systemctl is-enabled httpd
enabled

Unit的再启用(reenable)

使用再启动命令后,会先禁用Unit后再启用。

1
2
3
$ sudo systemctl reenable httpd
Removed symlink /etc/systemd/system/multi-user.target.wants/httpd.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.

启动Unit(start)

service命令会显示启动过程,而systemctl则并不显示启动的详细过程。

1
2
3
4
$ sudo systemctl start httpd
$ ps aux | grep httpd
root      7977  0.3  0.4 213704  4880 ?        Ss   01:02   0:00 /usr/sbin/httpd -DFOREGROUND
apache    7978  0.0  0.2 213704  2872 ?        S    01:02   0:00 /usr/sbin/httpd -DFOREGROUND

Unit的状态确认(status)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ sudo systemctl status httpd
httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled)
   Active: active (running) since 金 2014-10-10 01:06:07 UTC; 8s ago
 Main PID: 7998 (httpd)
   Status: "Processing requests..."
   CGroup: /system.slice/httpd.service
           ├─7998 /usr/sbin/httpd -DFOREGROUND
           ├─7999 /usr/sbin/httpd -DFOREGROUND
           ├─8000 /usr/sbin/httpd -DFOREGROUND
           ├─8001 /usr/sbin/httpd -DFOREGROUND
           ├─8002 /usr/sbin/httpd -DFOREGROUND
           └─8003 /usr/sbin/httpd -DFOREGROUND
10月 10 01:06:07 ip-172-31-7-131 systemd[1]: Starting The Apache HTTP Server...
10月 10 01:06:07 ip-172-31-7-131 systemd[1]: Started The Apache HTTP Server.

显示Unit的详细信息(show)

show选项会显示Unit的详细信息。虽然status选项更加直观,但在处理脚本的时候show选项使用起来更加得心应手。

1
2
3
4
5
6
7
8
9
10
11
12
$ sudo 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
...

终止Unit(stop)

终止单元和其开启过程一样,并不显示详细信息。

1
$ sudo systemctl stop httpd

重新读取Unit的配置文件(reload)

是否可重新加载取决于Unit自身。

1
$ sudo systemctl reload httpd

重启Unit(restart)

startstop一样,通常无消息提示。

1
$ sudo systemctl restart httpd

尝试重启Unit(try-restart)

Unit处在启动状态时则重启。Unit未运行时则不进行任何操作。

1
$ sudo systemctl try-restart httpd

重新加载Unit设置或重启(reload-or-restart)

若Unit能重新加载配置则执行reload操作,若无法重新加载配置则执行restart操作。另外Unit若处在停止状态则启动。

1
$ sudo systemctl reload-or-restart httpd

重新载入Unit的配置或尝试重启(reload-or-try-restart)

虽然与reload-or-restart同样,但若Unit处在停止状态时则不启动。

1
2
3
4
5
$ sudo systemctl reload-or-try-restart httpd
$ sudo systemctl stop httpd
$ sudo systemctl reload-or-try-restart httpd
Job for httpd.service failed. See 'systemctl status httpd.service' and 'journalctl -xn' for details.

强行终止Unit(kill)

1
$ sudo systemctl kill httpd

Unit的锁定(遮掩)(mask/unmask)

虽然禁用(disable)Unit后也能启动它,但使用mask命令后该服务则完全无法启动。

1
2
3
4
$ sudo systemctl mask httpd
ln -s '/dev/null' '/etc/systemd/system/httpd.service'
$ sudo systemctl start httpd
Failed to issue method call: Unit httpd.service is masked.

Unit被masked后,is-enabled会提示被锁定。

1
2
$ sudo systemctl is-enabled httpd
masked

解除mask需要使用unmask选项。

1
2
$ sudo systemctl unmask httpd
rm '/etc/systemd/system/httpd.service'

确认Unit的运行状态(is-active)

使用is-active选项,若Unit处在运行状态则反馈active。

1
2
3
$ sudo systemctl start httpd
$ sudo systemctl is-active httpd
active

若未处在运行状态则反馈unknown。

1
2
3
$ sudo systemctl stop httpd
$ sudo systemctl is-active httpd
unknown

查看Unit的异常状态(is-failed)

使用选项is-failed,若Unit正常运行则反馈active。

1
2
$ sudo systemctl is-failed httpd
active

若该Unit异常或未启动,则反馈failed。

1
2
3
4
5
$ sudo mv /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.org
$ sudo systemctl restart httpd
Job for httpd.service failed. See 'systemctl status httpd.service' and 'journalctl -xn' for details.
$ sudo systemctl is-failed httpd
failed

重置Unit的异常状态(reset-failed)

systemd将重置处在异常状态中的Unit。

1
2
3
4
5
$ sudo systemctl is-failed httpd
failed
$ sudo systemctl reset-failed httpd
$ sudo systemctl is-failed httpd
unknown

查看Unit的依赖关系(list-dependencies)

将显示该Unit所依赖的所有Unit。

1
2
3
4
5
6
7
8
9
10
11
12
$ sudo systemctl list-dependencies httpd
httpd.service
├─system.slice
└─basic.target
  ├─microcode.service
  ├─rhel-autorelabel-mark.service
  ├─rhel-autorelabel.service
  ├─rhel-configure.service
  ├─rhel-dmesg.service
  ├─rhel-loadmodules.service
  ├─paths.target
  ├─slices.target

写在最后

从CentOS 6.x过来的用户可能完全习惯了init的管理方式,对于新systemd的管理方式可能难以适应或心生厌恶。但由于systemd的强大和不断普及,今后将很有可能成为发展主流,所以有必要深入学习并习惯这种新管理方式。

本文来自投稿,不代表Linux运维部落立场,如若转载,请注明出处:http://www.178linux.com/100908

发表评论

登录后才能评论

联系我们

400-080-6560

在线咨询:点击这里给我发消息

邮件:1823388528@qq.com

工作时间:周一至周五,9:30-18:30,节假日同时也值班