从 CentOS 7.x 以后,Red Hat 系列的 distribution 放弃沿用多年的 System V 开机启动服务的流程,就是init 启动脚本的方法,改用 systemd 这个启动服务管理机制;
基本上,systemd 将过去所谓的 daemon 执行脚本通通称为一个服务单位 (unit),而每种服务单位依据功能来区分时,就分类为不同的类型 (type)。 基本的类型有包括系统服务、数据监听与交换的插槽档服务 (socket)、储存系统状态的快照类型、提供不同类似执行等级分类的操作环境 (target) 等等。 哇!这么多类型,那设定时会不会很麻烦呢?其实还好,因为配置文件都放置在底下的目录中:
l /usr/lib/systemd/system/:每个服务最主要的启动脚本设定,有点类似以前的 /etc/init.d 底下的文件;
l /run/systemd/system/:系统执行过程中所产生的服务脚本,这些脚本的优先序要比 /usr/lib/systemd/system/ 高!
l /etc/systemd/system/:管理员依据主机系统的需求所建立的执行脚本,其实这个目录有点像以前/etc/rc.d/rc5.d/Sxx 之类的功能!执行优先序又比 /run/systemd/system/ 高喔!
1) systemd 的 unit 类型分类说明
/usr/lib/systemd/system/ 以下的数据如何区分上述所谓的不同的类型 (type) 呢?很简单!看扩展名!
|
扩展名 |
主要服务功能 |
|
.service |
一般服务类型 (service unit):主要是系统服务,包括服务器本身所需要的本地服务以及网络服务都 是!比较经常被使用到的服务大多是这种类型! 所以,这也是最常见的类型了! |
|
.socket |
内部程序数据交换的插槽服务 (socket unit):主要是 IPC (Inter-process communication) 的传输讯息插槽文件 (socket file) 功能。 这种类型的服务通常在监控讯息传递的插槽文件,当有透过此插槽文件传递讯息来说要链接服务时,就依据当时的状态将该用户的要求传送到对应的 daemon,若daemon尚未启动,则启动该daemon后再传送用户的要求。 使用 socket 类型的服务一般是比较不会被用到的服务,因此在开机时通常会稍微延迟启动的时间 (因为比较没有这么常用嘛!)。一般用于本地服务比较多,例如我们的图形界面很多的软件都是透过 socket来进行本机程序数据交换的行为。(这与早期的 xinetd 这个 super daemon 有部份的相似喔!) |
|
.target |
执行环境类型 (target unit):其实是一群 unit 的集合,例如上面表格中谈到的 multi-user.target 其实 就是一堆服务的集合~也就是说, 选择执行 multi-user.target 就是执行一堆其他 .service 或/ 及 .socket 之类的服务就是了! |
|
.mount .automount |
文件系统挂载相关的服务 (automount unit / mount unit):例如来自网络的自动挂载、NFS 文件系统 挂载等与文件系统相关性较高的程序管理。 |
|
.path |
侦测特定文件或目录类型 (path unit):某些服务需要侦测某些特定的目录来提供队列服务,例如最 常见的打印服务,就是透过侦测打印队列目录来启动打印功能! 这时就得要 .path 的服务类型支持 了! |
|
.timer |
循环执行的服务 (timer unit):这个东西有点类似 anacrontab 喔!不过是由 systemd 主动提供的, 比 anacrontab 更加有弹性! |
2) 如何启动/关闭、启用/禁用服务
[root@study ~]# systemctl [command] [unit]
command 主要有:
start :立刻启动后面接的 unit stop :立刻关闭后面接的 unit
restart :立刻关闭后启动后面接的 unit,亦即执行 stop 再 start 的意思
reload :不关闭后面接的 unit 的情况下,重载配置文件,让设定生效
enable :设定下次开机时,后面接的 unit 会被启动
disable :设定下次开机时,后面接的 unit 不会被启动
status :目前后面接的这个 unit 的状态,会列出有没有正在执行、开机预设执行否、登录等信息等!
is-active :目前有没有正在运作中
is-enable :开机时有没有预设要启用这个 unit
例如:
启动一个服务:systemctl start postfix.service
关闭一个服务:systemctl stop postfix.service
重启一个服务:systemctl restart postfix.service
显示一个服务的状态:systemctl status postfix.service
在开机时启用一个服务:systemctl enable postfix.service
在开机时禁用一个服务:systemctl disable postfix.service
查看服务是否开机启动:systemctl is-enabled postfix.service
查看已启动的服务列表:systemctl list-unit-files|grep enabled
查看启动失败的服务列表:systemctl --failed
PS:使用命令 systemctl is-enabled postfix.service 得到的值可以是enable、disable或static,这里的 static 它是指对应的 Unit 文件中没有定义[Install]区域,因此无法配置为开机启动服务。说明:启用服务就是在当前“runlevel”的配置文件目录/etc/systemd/system/multi-user.target.wants/里,建立/usr/lib/systemd/system里面对应服务配置文件的软链接;
禁用服务就是删除此软链接,
添加服务就是添加软连接。如下:
#systemctl disable nfs-server.service
rm '/etc/systemd/system/multi-user.target.wants/nfs-server.service'
#systemctl enable nfs-server.service
ln -s '/usr/lib/systemd/syste/nfs-server.service' '/etc/systemd/system/multi-user.target.wants/nfs-server.service'
3) 查看所有unit服务
[root@study ~]# systemctl
[root@study ~]# systemctl list-unit-files
[root@study ~]# systemctl list-units --type=service --all
[root@study ~]#systemctl list-units --type=target --all
systemd 也提供了数个简单的指令给我们切换操作模式之用喔! 大致上如下所示:
[root@study ~]# systemctl poweroff 系统关机
[root@study ~]# systemctl reboot 重新启动
[root@study ~]# systemctl suspend 进入暂停模式
[root@study ~]# systemctl hibernate 进入休眠模式
[root@study ~]# systemctl rescue 强制进入救援模式
[root@study ~]# systemctl emergency 强制进入紧急救援模式
4) 查看服务依赖性
systemctl list-dependencies [unit] [--reverse]
[root@study ~]# systemctl list-dependencies –reverse
选项与参数: --reverse :反向追踪谁使用这个 unit 的意思!
systemd-cgls以树形列出正在运行的进程,它可以递归显示控制组内容。
参考文献:
【1】 RHEL 7 中 systemctl 的用法(替代service 和 chkconfig),http://blog.csdn.net/catoop/article/details/47318225
【2】 systemctl 命令完全指南,https://linux.cn/article-5926-1.html#4_1552
【3】 鸟哥的Linux私房菜-基础篇.第四版,17.2 透过 systemctl 管理服务
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/16976507/viewspace-2130219/ ,如需转载,请注明出处,否则将追究法律责任。