Linux操作系统的系统和服务管理器, 向后兼容SysV init脚本,提供很多功能:启动时并行启动系统服务、按需激活守护进程,基于依赖的服务,替换Upstart默认初始化系统systemd units概念,以unit配置文件形式列出,封装系统服务信息,监听sockets等等,/etc/systemd/system.conf作为systemd的配置文件主要功能:Socket-based activation : 启动时刻,systemd为满足改激活模式的服务建立sockets, 并当服务启动后进行连接,这样不仅可以以并行模式启动服务,而且当服务中断时不会丢失数据,外部数据仍处于sockets中并以队列方式等待连接。
可用的systemd units类型:
| unit type | File Extension | Description |
|---|---|---|
| Service unit | .service | a system service |
| target unit | .target | a group of systemd unit |
| Socket unit | .socket | 内部进程通信的socket |
| … |
systemd units位置:
| Path | Description |
|---|---|
| /usr/lib/systemd/system/ | 安装rpm包分发的系统units |
| /run/systemd/system/ | 运行时创建的systemd units 文件,优先级比前者高 |
| /etc/systemd/system/ | 通过systemctl enable创建的systemd units符号链接文件,优先级比前者高 |
Systemctl
systemctl的作用?
systemctl用于管理service, systemctl用于控制着不同的service, 这些service用于启动不同的任务。
相关操作?
- 停止服务
systemctl stop vsftpdvsftpd可以是别名,使用以下命令查看: systemctl show vstfpd -p Names
相关命令
systemctl try-start <name> # 当且仅当服务处于running状态重启service systemctl reload <name> # 重新加载配置文件,并不会中断service执行。 # system service不支持该选项请使用reload-or-restart或reload-or-try-restart选项 systemctl status <name> systemctl is-active <name> # 查询服务是否运行正常, 正常则返回状态为0 systemctl is-enable <name> # 服务是否开机启动, 正常则返回状态为0 systemctl list-units --type service --all # 查看本机所有service的状态, 没有--all会查看所有loaded且状态非in-active的service systemctl list-unit-files --type service # 查询所有有效service units的状态为enabled/disabled systemctl list-dependencies --after | --before <name> # 查询service依赖的service项, --after表示在service启动前的依赖service systemctl start <name> systemctl restart <name> systemctl enable <name> # 相关操作如下: # 1. 读取service unit的Install Section # 2. 创建/usr/lib/systemd/system/name.service的符号链接文件到/etc/systemd/system/的对应子目录当中, 当符号链接文件已经存在不会创建 systemctl reenable <name> # disable再enable<name> systemctl disable <name> # 读取servoce Install Section并移除对应符号链接文件 systemctl mask <name> # 隐藏对应service, 创建/etc/systemd/system/name.service -> /dev/null systemctl unmask <name>dependencies
存在positive和negative dependencies两种,positive dependencies表示启动service会开启的其它服务,negative表示启动service会停止其它的服务。
在启动service, systemd会解析service dependencies并控制对应依赖项。
