设为首页 收藏本站
查看: 654|回复: 0

[经验分享] 操作系统CnetOS_7—systemd管理实践指南

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-9-22 08:46:07 | 显示全部楼层 |阅读模式
管理systemd
[size=1em]CentOS 7 使用systemd替换了SysV。Systemd目的是要取代Unix时代以来一直在使用的init系统,兼容SysV和LSB的启动脚本,而且够在进程启动过程中更有效地引导加载服务。
[size=1em]systemctl命令是系统服务管理器指令,它实际上将 service 和 chkconfig 这两个命令组合到一起。
[size=1em]Systemd :系统启动和服务器守护进程管理器,负责在系统启动或运行时,激活系统资源,服务器进程和其它进程
Systemd 新特性:
系统引导时实现服务并行启动
按需启动守护进程
自动化 的 服务 依赖 关系管理
同时 采用socket 式与D-Bus 总线式激活 服务
系统状态快照
核心概念:unit
[size=1em]unit 表示不同类型的systemd 对象,通过配置文件进行标识和配置;文件中主要包含了系统服务、监听socket 、保存的系统快照以及其它与init
配置文件:
/usr/lib/systemd/system: 每个 服务最主要的启动脚本设置 ,类似于之前的/etc/init.d/
/run/systemd/system :系统执行过程中所产生的服务脚本,比上面目录优先运行
/etc/systemd/system :管理员建立的执行脚本,类似于 于/etc/rc.d/rcN.d/Sxx 类的功能,比上面目录优先运行
Unit类型
Systemctl –t help 查看unit 类型
[iyunv@localhost ~]# systemctl -t help
Available unit types:
service
socket
busname
target
snapshot
device
mount
automount
swap
timer
path
slice
scope
[iyunv@localhost ~]# ll /usr/lib/systemd/system | head -5
total 1220
-rw-r--r--. 1 root root  275 Dec  1  2015 abrt-ccpp.service
-rw-r--r--. 1 root root  380 Dec  1  2015 abrtd.service
-rw-r--r--. 1 root root  361 Dec  1  2015 abrt-oops.service
-rw-r--r--. 1 root root  266 Dec  1  2015 abrt-pstoreoops.service
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 ,用于定义文件系统中的一个文件或目录使用,常 常用于当文件系统变化时,延迟激活服务,如:spool 目录
特性
关键特性:
基于socket 的激活机制:socket 与服务程序分离
基于d-bus 的激活机制:
基于device 的激活机制:
基于path 的激活机制:
系统快照:保存各unit 的当前状态信息于持久存储设备中向后兼容sysv init 脚本
不兼容:
systemctl 命令固定不变,不可扩展
非由systemd 启动的服务,systemctl 无法与之通信和控
管理服务管理服务
命令格式:systemctl COMMAND name.service
注意:以下name.service表示某个具体服务
启动: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 cond restart ==> systemctl try-restart name.service
重载或重启服务:先加载,再启动: systemctl reload-or-restart name.service
重载或条件式重启服务:systemctl reload-or-try-restart name.service
禁止自动和手动启动:systemctl mask name.service
取消禁止:systemctl unmask name.service
服务查看##查看 某服务当前激活与否的状态:  systemctl is-active name.service
##查看所有已经激活的服务:   systemctl list-units --type|-t service
##查看所有服务:  systemctl list-units --type service -a
服务状态systemctl list-units --type service --all 显示状态
loaded:Unit 配置文件已处理
active(running): 一次或多次持续处理的运行
active(exited): 成功完成一次性的配置
active(waiting): 运行中,等待一个事件
inactive: 不运行
enabled: 开机启动
disabled: 开机不启动
static:开机不启动,但可被另一个启用的服务激活
杀掉进程:
systemctl kill  进程名
chkconfig 命令的对应关系:设定某服务开机自启:
chkconfig name on ==> systemctl enable name.service
设定某服务开机禁止启动:
chkconfig name off ==> systemctl disable name.service
查看所有服务的开机自启状态:
chkconfig --list ==> systemctl list-unit-files --type service
wKioL1ficNrgSg5mAABghFZVpvs429.jpg
用来列出该服务在哪些运行级别下启用和 禁用
chkconfig sshd –list ==> ls /etc/systemd/system/*.wants/sshd.service
[iyunv@localhost ~]# ls /etc/systemd/system/*.wants/sshd.service
/etc/systemd/system/multi-user.target.wants/sshd.service
查看服务是否开机自启:
systemctl is-enabled name.service
[iyunv@localhost ~]# systemctl is-enabled sshd.service
enabled
其它命令:
查看服务的依赖关系:
systemctl list-dependencies name.service
wKiom1ficPjC2r3pAABQv-mZXUY810.jpg
运行级别
target units:
unit 配置文件:.target
ls /usr/lib/systemd/system/*.target
systemctl list-unit-files --type target --all //查看级别
运行级别:
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
查看依赖性:
systemctl list-dependencies graphical.target   //查看级别的依赖性
wKioL1ficRXSZsH_AACmhehEKWY649.jpg
级别切换
级别切换:init N ==> systemctl isolate name.target
systemctl isolate multi-user.target  //切换到3级别
[size=1em]注: 只有/lib/systemd/system/*.target 文件中AllowIsolate=yes 才能切换( 修改文件需执行systemctl daemon-reload 才能生效)
wKiom1ficSvj_j1YAACdum0jAM8899.jpg
获取默认运行级别
/etc/inittab ==> systemctl get-default
[iyunv@localhost ~]# systemctl get-default
multi-user.target
修改默认级别
/etc/inittab ==> systemctl set-default name.target
systemctl set-default multi-user.target
ls –l /etc/systemd/system/default.target
wKiom1ficUDDcdMQAAAt8VHqkgg705.jpg
其它命令#切换至紧急救援模式:
systemctl rescue
#切换至emergency 模式:
systemctl emergency
其它常用命令:
传统命令init ,poweroff ,halt ,reboot 都成为 systemctl 的软链接
关机:systemctl halt 、systemctl poweroff
重启:systemctl reboot
挂起:systemctl suspend
休眠: :systemctl hibernate
休眠并挂起:systemctl hybrid-sleep
service unit 文件格式 /etc/systemd/system :系统管理员和用户使用
/usr/lib/systemd/system :发行版打包者使用
实例:
[iyunv@localhost ~]# 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
service unit file 文件通常由三部分组成:
[size=1em][Unit] :定义与Unit 类型无关的通用选项;用于提供unit 的描述信息、unit 行为及依赖关系等
[Service] :与特定类型相关的专用选项;此处为Service 类型
[Install] :定义由“systemctl enable” 以及”systemctl disable“
Unit 段的常用选项:
Description :描述信息
After :定义unit 的启动次序,表示当前unit 应该晚于哪些unit 启动,其功能与Before 相反
Requires :依赖到的其它units ,强依赖,被依赖的units 无法激活时,当前unit 即无法激活
Wants :依赖到的其它units, , 弱依赖
Conflicts :定义units
Service 段的常用选项:
Type :定义影响ExecStart 及相关参数的功能的unit 进程启动类型
simple :默认值,这个daemon 主要由ExecStart 接的指令串来启动,启动后常驻于内存中
forking :由ExecStart 启动的程序透过spawns 延伸出其他子程序来作为此daemon 的主要服务。原生父程序在启动结束后就会终止
oneshot :与simple 类似,不过这个程序在工作完毕后就结束了,不会常驻在内存中
dbus :与simple 类似,但这个daemon 必须要在取得一个D-Bus的 的名称后,才会继续运作. 因此通常也要同时设定BusNname=  才行
notify :在启动完成后会发送一个通知消息。还需要配合NotifyAccess 让 来让 Systemd  接收消息
idle :与simple 类似,要执行这个daemon 必须要所有的工作都顺利执行完毕后才会执行。这类的daemon 通常是开机到最后才执行即可的服务
EnvironmentFile :环境配置文件
ExecStart :指明启动unit 要运行命令或脚本的绝对路径
ExecStartPre: : ExecStart 前运行
ExecStartPost: : ExecStart 后运行
ExecStop :指明停止unit 要运行的命令或脚本
Restart :当设定Restart=1  时,则当次daemon 服务意外终止后,会再次自动启动此服务
Install 段的常用选项:
Alias :别名,可使用systemctl command Alias.service
RequiredBy :被哪些units 所依赖,强依赖
WantedBy :被哪些units 所依赖,弱依赖
Also :安装本服务的时候还要安装别的相关服务
注意:对于新创建的unit 文件,或者修改了的unit 文件,要通知systemd 重载此配置文件, 而后可以选择重启 systemctl daemon-reload
服务Unit 文件示例
(1)创建一个脚本,用于被创建的服务调用
[iyunv@localhost system]# cat /testdir/bak.sh
#!/bin/bash
# 备份/etc/目录
tar -Jcvf /testdir/etc-`date +%F`.tar.xz /etc/ &> dev/null
(2)给bak.sh脚本添加执行权限
[iyunv@localhost ~]# chmod u+x /testdir/bak.sh
(3)创建bak.service服务
[iyunv@localhost ~]# vim /etc/systemd/system/bak.service
[Unit]
Description=backup my etc
Requires=atd.service
[Service]
Type=simple
ExecStart=/bin/bash -c "echo /testdir/bak.sh|at now"
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl start bak
(4)启用服务
[iyunv@localhost system]# systemctl daemon-reload
[iyunv@localhost system]# systemctl start bak
(5)验证
[iyunv@localhost system]# ll /testdir/
total 8132
-rwxr--r-- 1 root root      91 Sep 21 19:14 bak.sh
-rw-r--r-- 1 root root 4546560 Sep 21 19:15 etc-2016-09-21.tar.xz

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-275688-1-1.html 上篇帖子: redhat linux的基本操作 下篇帖子: 一次测试环境下的高可用NFS文件服务器(DRBD+heartbeat+NFS) 操作系统
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表