86 lines
4.7 KiB
Markdown
86 lines
4.7 KiB
Markdown
从CentOS7开始,使用systemd实现init进程,系统启动和服务器守护进程管理器,负责在系统启动或者运行时,激活系统资源,服务器进程和其他进程。
|
||
## 1、Unit类型
|
||
unit表示不同类型的systemd对象,通过配置文件进行标识和配置,文件中主要包含了系统服务、监听socket、保存的系统快照以及其他与init相关的进程
|
||
```shell
|
||
[root@centos ~]#systemctl -t help
|
||
Available unit types:
|
||
service #文件拓展名为.service,用于定义系统服务
|
||
socket #定义进程间通信用的socket文件,也可在系统启动时,延迟启动服务,实现按需启动
|
||
target #文件扩展名为.target,用于模拟实现运行级别
|
||
snapshot #管理系统快照
|
||
device #用于定义内核识别的设备
|
||
mount #定义文件系统挂载点
|
||
automount #文件系统的自动挂载点
|
||
swap #用于表示swap设备
|
||
```
|
||
```shell
|
||
/usr/lib/systemd/system #每个服务最主要的启动脚本
|
||
/lib/systemd/system #ubuntu的对应目录
|
||
|
||
/run/systemd/system #系统执行过程中所产生的服务脚本,比上面的目录优先运行
|
||
/etc/systemd/system #管理员建立的执行脚本,比上面的目录优先运行
|
||
```
|
||
## 2、unit文件格式
|
||
/etc/systemd/system:系统管理员和用户使用
|
||
/usr/lib/systemd/system:发行版打包者使用
|
||
```shell
|
||
# /usr/lib/systemd/system/sshd.service
|
||
[Unit]:定义与unit类型无关的通用选项,用于提供unit的描述信息、unit行为及依赖关系等
|
||
Description=OpenSSH server daemon #描述信息
|
||
Documentation=man:sshd(8) man:sshd_config(5)
|
||
Requires=network.target #依赖到的其他units,强依赖,被依赖的服务无法激活时,当前unit也无法激活
|
||
After=network.target sshd-keygen.service #定义unit的启动次序,表示当前unit应该晚于哪些unit启动
|
||
Wants=sshd-keygen.service #依赖到的其他units,弱依赖
|
||
|
||
[Service]:与特定类型相关的专用选项
|
||
Type=notify #定义影响ExecStart及相关参数的功能的unit进程启动类型
|
||
simple:默认值,这个daemon主要由ExecStart接的指令串来启动,启动后常驻于内存中
|
||
forking:由ExecStart启动的程序透过spawns眼神出其他子程序来作为此daemon的主要服务。原生父程序在启动结束后就会结束
|
||
oneshot:与simple类似,不过这个程序在工作完成后就结束了,不会常驻在内存中
|
||
dbus:与simple相似,但是这个daemon必须要在取得一个D-Bus的名称后,才会继续运行,因此通常要设定BusName=才行
|
||
notify:在启动完成后会发送一个通知信息,还需要配合NotifyAccess来让systemd接收消息
|
||
idle:与simple类似,要执行这个daemon必须要所有的工作都顺利执行完毕后才会执行
|
||
EnvironmentFile=/etc/sysconfig/sshd #环境配置文件
|
||
ExecStartPre= #ExecStart前运行
|
||
ExecStartPost= #ExecStart后运行
|
||
ExecStart=/usr/sbin/sshd -D $OPTIONS #启动unit要运行命令或脚本的绝对路径
|
||
ExecReload=/bin/kill -HUP $MAINPID
|
||
KillMode=process
|
||
Restart=on-failure #失败后会再次自动启动此服务
|
||
RestartSec=42s #重启服务前暂停多长时间
|
||
|
||
[Install]:定义由"systemctl enbale" 以及"systemctl disable" 命令在实现服务启用或禁用时用到的一些选项
|
||
WantedBy=multi-user.target #被那些units所依赖,弱依赖
|
||
Alias= #别名
|
||
RequiredBy= #被哪些units所依赖,强依赖
|
||
|
||
```
|
||
2、常用命令
|
||
```shell
|
||
[root@centos ~]#systemctl list-units -t service
|
||
UNIT LOAD ACTIVE SUB DESCRIPTION
|
||
acpid.service loaded active running ACPI Event Daemon
|
||
atd.service loaded active running Job spooling tools
|
||
auditd.service loaded active running Security Auditing Service
|
||
ccsp.service loaded active running fraps service
|
||
cloud-config.service loaded active exited Apply the settings specified in cloud-config
|
||
cloud-final.service loaded active exited Execute cloud user/final scripts
|
||
|
||
[root@centos ~]#systemctl list-unit-files -t service
|
||
UNIT FILE STATE
|
||
abrt-ccpp.service disabled
|
||
abrt-oops.service disabled
|
||
abrt-pstoreoops.service disabled
|
||
abrt-vmcore.service disabled
|
||
|
||
loaded:unit配置文件已处理
|
||
active(running) 一次或多次持续处理的音响
|
||
active(exited)成功完成一次性的配置
|
||
active(waiting)运行中,等待一个事件
|
||
inactive 不允许
|
||
enbaled 开机启动
|
||
disabled 开机不启动
|
||
static 开机不启动,但可以被另外一个启动的服务激活
|
||
indirect 重定向到别处
|
||
```
|