Systemd程序及相关命令
Systemd是一款用于Linux操作系统系统管理和服务管理的工具。它向后兼容SysV init脚本,并且支持许多类似于startup系统服务的功能,比如系统快照(snapshots)。在RHEL 7中,systemd替代了Upstarts。
主要特性
基于socket的激活机制
基于bus的激活机制
基于device的激活机制
基于path的激活机制
支持系统状态快照
支持挂载和自动挂载点
支持并行化
开闭单元逻辑事务化
向后兼容SysV init
Systemd单元类型
单元类型 文件后缀 描述
Service unit .service 单个系统服务
Target unit .target 一组systemd单元
Automount unit .automount 一个文件系统挂载点
Device unit .device 内核识别的设备文件
Path unit .path 文件或目录
Scope unit .scope 由外部创建的进程
Slice unit .slice 一组具有层级结构、管理系统进程的单元
Snapshot unit .snapshot systemd 管理器保存的一种状态
Socket unit .socket 一种进程间通信socket
Swap unit .swap 交换设备或者交换文件
TImer unit .timer systemd计时器
相关文件
/usr/lib/systemd/system/ 安装时的配置文件
/run/systemd/system 相关单元在运行时产生的文件
/etc/systemd/system/ systemctl enable 命令产生的文件
使用systemd管理服务
systemctl start name.service
systemctl status name.service
systemctl stop name.service
systemctl restart name.service
systemctl try-restart name.service
systemctl reload name.service
systemctl is-active name.service
systemctl list-units [--type service --all ]
systemctl enable name.service
systemctl disable name.service
systemctl is-enabled name.service
systemctl list-unit-files --type service
使用systemd管理target
CentOS 7中的运行target与CentOS6中的运行等级类似,并且兼容命令init和runlevel
0 runlevel0.target, poweroff.target Shut down and power off the system.
1 runlevel1.target, rescue.target Set up a rescue shell.
2 runlevel2.target, multi-user. target Set up a non-graphical multi-user system.
3 runlevel3.target, multi-user.target Set up a non-graphical multi-user system.
4 runlevel4.target,multi-user. target Set up a non-graphical multi-user system.
5 runlevel5.target,graphical.target Set up a graphical multi-user system.
6 runlevel6.target,reboot.target Shut down and reboot the system.
查看运行等级
systemctl get-default
设置运行等级
systemctl set-default name.target
改变当前运行等级
systemctl isolate name.target
systemctl [rescue | halt | reboot | poweroff | hibernate | suspend]
Systemd 单元文件
组成
[Unit]
包含通用的选项,包括描述、依赖的单元等
[unit type]
单元类型
[Install]
包含单元安装信息
常用选项
Description
Documentation
After
Requires
Wants
Conflicts
Type
ExecStart
ExecStop
ExecReload
Restart
RemainAfterExit
Alias
示例
[root@host ~]#cat /usr/lib/systemd/system/httpd.service [Unit] Description=The Apache HTTP Server After=network.target remote-fs.target nss-lookup.target Documentation=man:httpd(8) Documentation=man:apachectl(8) [Service] Type=notify EnvironmentFile=/etc/sysconfig/httpd ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND ExecReload=/usr/sbin/httpd $OPTIONS -k graceful ExecStop=/bin/kill -WINCH ${MAINPID} # We want systemd to give httpd some time to finish gracefully, but still want # it to kill httpd after TimeoutStopSec if something went wrong during the # graceful stop. Normally, Systemd sends SIGTERM signal right after the # ExecStop, which would kill httpd. We are sending useless SIGCONT here to give # httpd time to finish. KillSignal=SIGCONT PrivateTmp=true [Install] WantedBy=multi-user.target