CentOS7开机启动管理systemd简介及使用

systemd提供更优秀的框架以表示系统服务间的依赖关系
实现系统初始化时服务的并行启动,同时达到降低Shell的系统开销的效果
systemd的目标是:尽可能启动更少进程;尽可能将更多进程并行启动。
systemd尽可能减少对shell脚本的依赖。

systemd单位类型

(systemctl --type=单位类型,用来过滤单位):
服务(service):管理着后台服务;
挂载(mount)自动挂载(automount):用来挂载文件系统;
目票(target):运行级别;
套接字(socket):用来创建套接字,并在访问套接字后,立即利用依赖关系间接地启动另一单位

Unit段的常用选项:

Description:描述信息
After:定义unit的启动次序,表示当前unit应该晚于哪些unit启动,其功能与Before相反
Requires:依赖到的其它units,强依赖,被依赖的units无法激活时,当前unit也无法激活
Wants:依赖到的其它units,弱依赖
Conflicts:定义units间的冲突关系

[Service]:与特定类型相关的专用选项;此处为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通常是开机到最                  后才执行即可的服务

[Install]:定义由“systemctl enable”以及"systemctl disable“命令在实现服务启用或禁用时用到的一些选项

Alias:别名,可使用systemctl command Alias.service
RequiredBy:被哪些units所依赖,强依赖
WantedBy:被哪些units所依赖,弱依赖
Also:安装本服务的时候还要安装别的相关服务

查看systemd单元加载及活动情况

systemctl

显示启动失败的单元

systemctl --failed

查看systemd管理的所有单元

systemctl list-unit-files

服务管理

启动服务
systemctl start httpd.service
关闭服务
systemctl stop httpd.service
重启服务
systemctl restart httpd.service
重新加载
systemctl reload httpd.service
查看状态
systemctl status httpd.service

包括启动状态、启动时间、主进程及相关进程、相关日志

运行级别
systemd用target替代了runlevel的概念,多个的 'target' 可以同时激活
systemd不使用/etc/inittab,如何查看系统默认的运行级别

ll /etc/systemd/system/default.target

查看这个软链接真正指向的文件

如何查看系统的当前运行级别
runlevel依然可用

systemd的方法是:systemctl list-units --type=target

改变当前target,重启无效

systemctl isolate graphical.target

修改默认运行级别
1.首先删除已经存在的符号链接

rm /etc/systemd/system/default.target

2.默认级别转换为3(文本模式)

systemctl enable multi-user.target

相当于

ln -s /lib/systemd/system/multi-user.target /etc/systemd/system/default.target

3.重启

reboot

运行级别如下:

runlevel0.target -> poweroff.target
runlevel1.target -> rescue.target
runlevel2.target -> multi-user.target
runlevel3.target -> multi-user.target
runlevel4.target -> multi-user.target
runlevel5.target -> graphical.target
runlevel6.target -> reboot.target


内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://sulao.cn/post/333.html

我要评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。