Linux进程管理工具 Systemd 入坑指南

1.systemd 介绍

systemd是目前Linux系统上主要的系统守护进程管理工具,由于init一方面对于进程的管理是串行化的,容易出现阻塞情况,另一方面init也仅仅是执行启动脚本,并不能对服务本身进行更多的管理。所以从CentOS 7开始也由systemd取代了init作为默认的系统进程管理工具

systemd所管理的所有系统资源都称作Unit,通过systemd命令集可以方便的对这些Unit进行管理。比如systemctl、hostnamectl、timedatectl、localctl等命令,这些命令虽然改写了init时代用户的命令使用习惯(不再使用chkconfig、service等命令),但确实也提供了很大的便捷性。

2.systemd 特点

  • 最新系统都采用systemd管理(RedHat7,CentOS7,Ubuntu15…)
  • CentOS7 支持开机并行启动服务,显著提高开机启动效率
  • CentOS7关机只关闭正在运行的服务,而CentOS6,全部都关闭一次。
  • CentOS7服务的启动与停止不再使用脚本进行管理,也就是/etc/init.d下不在有脚本。
  • CentOS7使用systemd解决原有模式缺陷,比如原有service不会关闭程序产生的子进程。

3.systemd 语法

systemctl [command]      [unit](配置的应用名称)
  • command可选项

    · start:启动指定的unit          systemctl start nginx
    · stop:关闭指定的unit           systemctl stop nginx
    · restart:重启指定unit          systemctl restart nginx
    · reload:重载指定unit           systemctl reload nginx
    · enable:系统开机时自动启动指定unit,前提是配置文件中有相关配置 systemctl enable nginx
    · disable:开机时不自动运行指定unit   systemctl disable nginx
    · status:查看指定unit当前运行状态 systemctl status nginx

4.systemd 配置文件说明

这里我们先要说明一下unit的文件位置,一般主要有三个目录:

/lib/systemd/system
/run/systemd/system
/etc/systemd/system
  1. 这三个目录的配置文件优先级依次从低到高,如果同一选项三个地方都配置了,优先级高的会覆盖优先级低的。 系统安装时,默认会将unit文件放在/lib/systemd/system目录。
  2. 每一个 Unit 都需要有一个配置文件用于告知 systemd 对于服务的管理方式。
  3. 配置文件存放于/usr/lib/systemd/system/,设置开机启动后会在/etc/systemd/system 目录建立软链接文件。
  4. 每个Unit的配置文件配置默认后缀名为.service
  5. /usr/lib/systemd/system/ 目录中分为 systemuser 两个目录,一般将开机不登陆就能运行的程序存在系统服务里,也就是 /usr/lib/systemd/system
  6. 配置文件使用方括号分成了多个部分,并且区分大小写。
  7. /run/systemd/system这个目录一般是进程在运行时动态创建unit文件的目录,一般很少修改,除非是修改程序运行时的一些参数时,即Session级别的,才在这里做修改。

5.systemd 相关文件

systemd控制的相关文件 CentOS6 SentOS7
服务启动的脚本启动路径 /etc/init.d /usr/lib/systemd/system/
开机自启动服务存放路径 /etc/rcN.d /etc/systemd/system/multi-user.target.wants/
默认运行级别配置文件 /etc/inittab /etc/systemd/system/default.target

6.服务配置

每一个服务以.service结尾,一般会分为3部分:[Unit][Service][Install],就以nginx为例吧,具体内容如下:

[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

6.1 配置项说明

下面分别解释下着三部分的含义

[Unit]

  • Description : 服务的简单描述
  • Documentation : 服务文档
  • After= : 依赖,仅当依赖的服务启动之后再启动自定义的服务单元

[Service]

  • Type : 启动类型simple、forking、oneshot、notify、dbus
    • Type=simple(默认值):systemd认为该服务将立即启动,服务进程不会fork。如果该服务要启动其他服务,不要使用此类型启动,除非该服务是socket激活型
    • Type=forking:systemd认为当该服务进程fork,且父进程退出后服务启动成功。对于常规的守护进程(daemon),除非你确定此启动方式无法满足需求, 使用此类型启动即可。使用此启动类型应同时指定PIDFile=,以便systemd能够跟踪服务的主进程。
    • Type=oneshot:这一选项适用于只执行一项任务、随后立即退出的服务。可能需要同时设置 RemainAfterExit=yes 使得 systemd 在服务进程退出之后仍然认为服务处于激活状态。
    • Type=notify:与 Type=simple 相同,但约定服务会在就绪后向 systemd 发送一个信号,这一通知的实现由 libsystemd-daemon.so 提供
    • Type=dbus:若以此方式启动,当指定的 BusName 出现在DBus系统总线上时,systemd认为服务就绪。
  • PIDFile : pid文件路径
  • ExecStartPre :启动前要做什么,上文中是测试配置文件 -t
  • ExecStart:启动
  • ExecReload:重载
  • ExecStop:停止
  • PrivateTmp:True表示给服务分配独立的临时空间
  • User:服务启动用户
  • Group:服务启动用户组
  • WorkingDirectory:服务工作目录
  • Restart:重启策略,可选项:
    • no:不重启
    • always:总是
    • on-success:启动成功时
    • on-failure:启动失败时
    • on-abnormal:服务异常时
    • on-abort:服务异常退出时
    • on-watchdog:watchdog触发

[Install]

  • WantedBy:服务安装的用户模式,从字面上看,就是想要使用这个服务的有是谁?上文中使用的是:multi-user.target ,就是指想要使用这个服务的目录是多用户。

每一个.target实际上是链接到我们单位文件的集合,当我们执行

systemctl enable nginx.service

就会在 /etc/systemd/system/multi-user.target.wants/ 目录下新建一个 /usr/lib/systemd/system/nginx.service 文件的链接。

7.操作示例

# 自启动
systemctl enable nginx.service
# 禁止自启动
systemctl disable nginx.service
# 启动服务
systemctl start nginx.service
# 停止服务
systemctl stop nginx.service
# 重启服务
systemctl restart nginx.service

# 查看Unit定义文件
systemctl cat nginx.service
# 编辑Unit定义文件
systemctl edit nginx.service
# 重新加载Unit定义文件
systemctl reload nginx.service

# 列出已启动的所有unit,就是已经被加载到内存中
systemctl list-units
# 列出系统已经安装的所有unit,包括那些没有被加载到内存中的unit
systemctl list-unit-files

# 查看服务的日志
journalctl -u nginx.service    # 还可以配合`-b`一起使用,只查看自本次系统启动以来的日志

# 查看所有target下的unit
systemctl list-unit-files --type=target

# 查看默认target,即默认的运行级别。对应于旧的`runlevel`命令
systemctl get-default

# 设置默认的target
systemctl set-default multi-user.target

# 查看某一target下的unit
systemctl list-dependencies multi-user.target

# 切换target,不属于新target的unit都会被停止
systemctl isolate multi-user.target

systemctl poweroff    # 关机
systemctl reboot       # 重启
systemctl rescue    # 进入rescue模式

Linux进程管理工具 Systemd 入坑指南
https://www.gmtgo.com/115.html
作者
大帅
发布于
2022年8月22日
许可协议