19 Star 185 Fork 90

baoke / Linux学完就忘系列

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
39.systemd及systemctl实战.md 6.96 KB
一键复制 编辑 原始数据 按行查看 历史
baoke 提交于 2019-08-14 10:49 . 更新 39.systemd及systemctl实战.md

systemd:

POST --> Boot Sequence --> Bootloader(MBR) --> Kernel(ramdisk) --> rootfs --> /sbin/init
Init:
CentOS 5:SysV init
CentOS 6:Upstart
CentOS 7:Systemd

Systemd的新特性:

系统引导时实现服务并行启动;
按需激活进程;
系统状态快照;
基于依赖关系定义服务控制逻辑;

核心概念:unit

unit由其相关配置文件进行标识、识别和配置;文件中主要包含了系统服务、监听的socket、保存的快照以及其它与init相关的信息;这些配置文件主要保存在:
/usr/lib/systemd/system
/run/systemd/system
/etc/systemd/system

unit的常见类型:

Service unit:文件扩展名为.service,用于定义系统服务;
Target unit:文件扩展名为.target,用于模拟实现"运行级别";
Device unit:文件扩展名为.device,用于定义内核识别的设备;
Mount unit:文件扩展名为.mount,定义文件系统挂载点;
Socket unit:文件扩展名为.socket,用于标识进程间通信用到的socket文件;
Snapshot unit:文件扩展名为.snapshot,管理系统快照;
Swap unit:文件扩展名为.swap,用于标识swap设备;
Automount unit:文件扩展名为.automount,文件系统自动挂载点设置;
Path unit:文件扩展名为.path,用于定义文件系统中的一个文件或目录;

关键特性:

基于socket的激活机制:socket与程序分离;
基于bus(总线)的激活机制;
基于device的激活机制;
基于Path的激活机制;
系统快照:保存各unit的当前状态信息于持久存储设备中;
向后兼容sysv init脚本;/etc/init.d

不兼容:

systemctl的命令是固定不变的;
非由systemd启动的服务,systemctl无法与之通信;

管理系统服务:

CentOS 7:service类型的unit文件;

systemctl命令:

Control the systemd system and service manager

systemctl [OPTIONS...] COMMAND [NAME...]

启动:service NAME start ==> systemctl start NAME.service
停止:service NAME stop ==> systemctl stop NAME.service
重启:service NAME restart ==> systemctl restart NAME.service
状态:service NAME status ==> systemctl status NAME.service
条件式重启:service NAME condrestart ==> systemctl try-restart NAME.service
重载或重启服务:systemctl reload-or-restart NAME.service
注:重载代表重新读取配置文件而不用重启服务;重载或重启服务代表如果支持重载就重载,不支持就重启
重载或条件式重启服务:systemctl reload-or-try-restart NAME.service

查看某服务当前激活与否的状态:systemctl is-active NAME.service
查看所有已激活的服务:systemctl list-units -t service
查看所有服务(已激活及未激活):chkconfig --list ==> systemctl list-units --all --t service

设置服务开机自启:chkconfig NAME on ==> systemctl enable NAME.service
禁止服务开机自启:chkconfig NAME off ==> systemctl disable NAME.service
查看某服务是否能开机自启:chkconfig --list NAME ==> systemctl is-enabled NAME.service

禁止某服务设定为开启自启:systemctl mask NAME.service
取消此禁止:systemctl unmask NAME.service

查看服务的依赖关系:systemctl list-dependencies NAME.service

#查看所有已激活的服务
systemctl list-units --t service  
#查看所有服务(已激活及未激活)
systemctl list-units --all --t service
#查看某服务是否能开机自启
systemctl is-enabled nginx
#查看服务的依赖关系
systemctl list-dependencies nginx

service unit file:

service unit文件/usr/lib/systemd/system/nginx.service如下:

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

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target

文件通常由三部分组成:
[Unit]:定义与Unit类型无关的通用选项;用于提供unit的描述信息、unit行为及依赖关系等;
[Service]:与特定类型相关的专用选项;此处为Service类型,如果unit类型为Target,此处就为Target;
[Install]:定义由"systemctl enable"以及"systemctl disable"命令在实现服务启用或禁用时用到的一些选项;

Unit段的常用选项:
Description:描述信息;意义性描述;
After:定义unit的启动次序;表示当前unit应该晚于哪些unit启动;其功能与Before相反;
Requies:依赖到的其它units;(强依赖.被依赖的units无法激活时,当前units即无法激活);
Wants:依赖到的其它units;(弱依赖);
Conflicts:定义units间的冲突关系;

Service段的常用选项:
Type(了解即可):用于定义影响ExecStart及相关参数的功能的unit进程启动类型;
类型:
simple:代表ExecStart指定的程序为该服务的主进程
forking:代表ExecStart指定的程序所启动的进程,生成的一个子进程为该服务的主进程
oneshot:指启动一次,在启动后续的unit之前,主进程将会退出
dbus:后续的units仅在主进程得到dbus名称之后,才能启动
notify:类似于simple,后续的units仅在通过sdnotify函数发送通知以后,才能启动
idle:类似于simple,.....

EnvironmentFile:环境配置文件;
ExecStart:指明启动unit要运行命令或脚本;ExecStartPre,ExecStartPost
ExecStop:指明停止unit要运行的命令或脚本;
Restart:如果进程意外终止了,它会自动重启;

Install段的常用选项:
Alias:当前unit的别名;
RequiredBy:被哪些units所依赖(强依赖);
WantedBy:被哪些units所依赖(弱依赖);

注意:对于新创建的unit文件或修改了的unit文件,要通知systemd重载此配置文件;
systemctl daemon-reload

管理target units:

运行级别:
0 ==> runlevel0.target,poweroff.target
1 ==> runlevel1.target,rescue.target
2 ==> runlevel2.target,multi-user.target
3 ==> runlevel3.target,multi-user.target
4 ==> runlevel4.target,multi-user.target
5 ==> runlevel5.target,graphical.target
6 ==> runlevel6.target,reboot.target

级别切换:init N ==> systemctl isolate NAME.target

查看级别:runlevel ==> systemctl list-units -t target
查看所有级别:systemctl list-units -t target -a

获取默认运行级别:systemctl get-default
修改默认运行级别:systemctl set-default NAME.target

#查看激活的target unit
systemctl list-units -t target 
#查看所有的target unit
systemctl list-units -t target --all
#设置默认运行级别
systemctl set-default runlevel3
#级别切换
systemctl isolate runlevel3.target  

切换至紧急救援模式(运行级别1):systemctl rescue
切换至emergency模式:systemctl emergency

其他常用命令:

关机:systemctl halt,systemctl poweroff
重启:systemctl reboot
挂起:systemctl suspend
快照:systemctl hibernate
快照并挂起:systemctl hybrid-sleep

Shell
1
https://gitee.com/abcd1234567_1/linux.git
git@gitee.com:abcd1234567_1/linux.git
abcd1234567_1
linux
Linux学完就忘系列
master

搜索帮助