systemd
# systemd的由来
- Linux一直以来都是采用
init
进程作为祖宗进程,但是init
有两个缺点:- 启动时间长。
init
进程是串行启动,只有前一个进程启动完,才会启动下一个进程; - 启动脚本复杂。初始化完成后系统会加载很多脚本,脚本都会处理各自的情况,这会让脚本多而复杂;
- 启动时间长。
Centos5
是启动速度最慢的,串行启动过程,无论进程相互之间有无依赖关系;Centos6
相对启动速度有所改进,有依赖的进程之间依次启动而其他与之没有依赖关系的则并行同步启动;Centos7
所有进程无论有无依赖关系则都是并行启动(当然很多时候进程没有真正启动而是只有一个信号或者说是标记而已,在真正利用的时候才会真正启动);
# 什么是systemd
systemd
即为system daemon
守护进程,systemd
主要解决上文的问题而诞生;systemd
的目标是,为系统的启动和管理提供一套完整的解决方案;
# systemd的优势
- 最新系统都采用
systemd
管理(RedHat7,CentOS7,Ubuntu15等); Centos7
支持开机并行启动服务,显著提高开机启动效率;Centos7
关机只关闭正在运行的服务,而Centos6
全部都关闭一次;Centos7
服务的启动与停止不再使用脚本进行管理,也就是/etc/init.d
下不再有脚本;Centos7
使用systemd
解决原有模式缺陷,比如原有service
不会关闭程序产生的子进程;
# systemd配置文件
/usr/lib/systemd/system/
:类似Centos6
系统的启动脚本/etc/init.d/
/etc/systemd/system/
:类似Centos6
系统的/etc/rc.d/rcN.d/
/etc/systemd/system/multi-user.target.wants/
# systemd相关命令
systemctl
管理服务的启动、重启、停止、重载、查看状态等常用命令;systemctl命令 作用 systemctl start crond.service 启动服务 systemctl stop crond.service 停止服务 systemctl restart crond.service 重启服务 systemctl reload crond.service 重新加载配置 systemctl status crond.servre 查看服务运行状态 systemctl is-active sshd.service 查看服务是否在运行中 systemctl mask crond.servre 禁止服务运行 systemctl unmask crond.servre 取消禁止服务运行 当我们使用
systemctl
启动一个守护进程后,可以通过sysytemctl status
查看此守护进程的状态;状态 描述 loaded 服务单元的配置文件已经被处理 active(running) 服务持续运行 active(exited) 服务成功完成一次的配置 active(waiting) 服务已经运行但在等待某个事件 inactive 服务没有在运行 enabled 服务设定为开机运行 disabled 服务设定为开机不运行 static 服务开机不启动,但可以被其他服务调用启动 systemctl
设置服务开机启动、不启动、查看各级别下服务启动状态等常用命令;systemctl命令(7系统) 作用 systemctl enable crond.service 开机自动启动 systemctl disable crond.service 开机不自动启动 systemctl list-unit-files 查看各个级别下服务的启动与禁用 systemctl is-enabled crond.service 查看特定服务是否为开机自启动 systemctl daemon-reload 创建新服务文件需要重载变更 CentOS7
系统, 管理员可以使用systemctl
命令来管理服务器启动与停止;#关机相关命令 [root@web ~]# systemctl poweroff #立即关机,常用 #重启相关命令 [root@web ~]# systemctl reboot #重启命令,常用
1
2
3
4systemctl
的journalctl
日志;[root@web ~]# journalctl -n 20 #查看最后20行 [root@web ~]# journalctl -f #动态查看日志 [root@web ~]# journalctl -p err #查看日志的级别 [root@web ~]# journalctl -u crond #查看某个服务的单元的日志
1
2
3
4
# systemd管理运行级别
# 什么是运行级别
运行级别就是操作系统当前正在运行的功能级别;
System V init运行级别 systemd目标名称 作用 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 重启
# 调整系统启动的运行级别
systemd
使用targets
而不是runlevels
;默认情况下,有两个主要目标:
multi-user.target
:类似于运行级别3;graphical.target
:类似于运行级别5;
查看系统默认运行级别;
[root@web ~]# systemctl get-default
1要设置默认目标,请运行;
[root@web ~]# systemctl set-default TARGET.target
1
Last Updated: 2021/11/25, 10:57:04