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

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

systemd单位类型

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

Unit段的常用选项:

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

[Service]:与特定类型相关的专用选项;此处为Service类型

01.
Type:定义影响ExecStart及相关参数的功能的unit进程启动类型
02.
simple:默认值,这个daemon主要由ExecStart接的指令串来启动,启动后常驻于内存中
03.
forking:由ExecStart启动的程序透过spawns延伸出其他子程序来作为此daemon的主要服务。原生父程序在启动结束后                       就会终止
04.
oneshot:与simple类似,不过这个程序在工作完毕后就结束了,不会常驻在内存中
05.
dbus:与simple类似,但这个daemon必须要在取得一个D-Bus的名称后,才会继续运作.因此通常也要同时设定                                BusNname= 才行
06.
notify:在启动完成后会发送一个通知消息。还需要配合 NotifyAccess 来让 Systemd 接收消息
07.
idle:与simple类似,要执行这个daemon必须要所有的工作都顺利执行完毕后才会执行。这类的daemon通常是开机到最                  后才执行即可的服务

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

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

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

01.
systemctl

显示启动失败的单元

01.
systemctl --failed

查看systemd管理的所有单元

01.
systemctl list-unit-files

服务管理

01.
启动服务
02.
systemctl start httpd.service
03.
关闭服务
04.
systemctl stop httpd.service
05.
重启服务
06.
systemctl restart httpd.service
07.
重新加载
08.
systemctl reload httpd.service
09.
查看状态
10.
systemctl status httpd.service

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

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

01.
ll /etc/systemd/system/default.target

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

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

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

改变当前target,重启无效

01.
systemctl isolate graphical.target

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

01.
rm /etc/systemd/system/default.target

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

01.
systemctl enable multi-user.target

相当于

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

3.重启

01.
reboot

运行级别如下:

01.
runlevel0.target -> poweroff.target
02.
runlevel1.target -> rescue.target
03.
runlevel2.target -> multi-user.target
04.
runlevel3.target -> multi-user.target
05.
runlevel4.target -> multi-user.target
06.
runlevel5.target -> graphical.target
07.
runlevel6.target -> reboot.target


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

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

评论列表

0%