systemd service,rc.local查看启动顺序,调整启动顺序

您所在的位置:网站首页 配置systemctl和service启动服务 systemd service,rc.local查看启动顺序,调整启动顺序

systemd service,rc.local查看启动顺序,调整启动顺序

2024-07-13 18:09| 来源: 网络整理| 查看: 265

一、启动耗时分析,启动顺序分析

systemd-analyze blame  #列举出每个服务的启动耗时 systemd-analyze plot > boot.svg #图形化展示启动耗时

 可以看到各个service的启动顺序,不满意的,可以调整

二、service 设置依赖顺序

启动顺序的依赖在unit Section下面定义

Option

Description

Description

A short description of the unit.

Documentation

A list of URIs referencing documentation.

Before, After

The order in which units are started.

Requires

If this unit gets activated, the units listed here will be activated as well. If one of the other units gets deactivated or fails, this unit will be deactivated.

Wants

Configures weaker dependencies than Requires. If any of the listed units does not start successfully, it has no impact on the unit activation. This is the recommended way to establish custom unit dependencies.

Conflicts

If a unit has a Conflicts setting on another unit, starting the former will stop the latter and vice versa.

例如,若一个服务需要在网络基础设置ready的情况下再执行启动,可以这样编写

[Unit] Wants=network-online.target After=network.target network-online.target

这里wants指希望network-online.target能成功启动,一般非特殊情况很少使用到Requires强制依赖(即network-online.target一定要启动成功)

After指具体的顺序,服务在network.target network-online.target相关target启动完毕再进行启动

注意,network.target不能保证网络服务已经启动,正常情况应该依赖network-online.target,参考这里

官方文档建议,通过编写更健壮的程序,容忍网络的变化或者失败,才是最佳的解决方案(因为实际我们无法保证网络服务长期稳定不变的运行,在某个时段网络服务可能有一些变化,这个是比较普遍的想象)。

 [root@test2 ~]# vi /usr/lib/systemd/system/mysqld80.service #例子,在这个基础上调整

[Unit] Description=MySQL Server 8.0 Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html After=network.target After=syslog.target [Service] User=mysql Group=mysql LimitNOFILE=65536 ExecStart=/opt/mysql-8.0.32/bin/mysqld --defaults-file=/opt/mysql-8.0.32/my.cnf [Install] WantedBy=multi-user.target

[root@test3 ~]# vi /usr/lib/systemd/system/redis6.service  #例子,在这个基础上调整

[Unit] Description=redis6 After=network.target [Service] Type=forking LimitNOFILE=infinity ExecStart=/opt/redis6/bin/redis-server /opt/redis6/redis.conf ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/opt/redis6/bin/redis-cli -a redis6211 shutdown PrivateTmp=true [Install] WantedBy=multi-user.target 三、调整rc.local的启动顺序

linux启用rc.local

rc.local 也是受systemd管理的一个service,/usr/lib/systemd/system/rc-local.service

[root@dev2 multi-user.target.wants]# vi /usr/lib/systemd/system/rc-local.service

#  This file is part of systemd. # #  systemd is free software; you can redistribute it and/or modify it #  under the terms of the GNU Lesser General Public License as published by #  the Free Software Foundation; either version 2.1 of the License, or #  (at your option) any later version. # This unit gets pulled automatically into multi-user.target by # systemd-rc-local-generator if /etc/rc.d/rc.local is executable. [Unit] Description=/etc/rc.d/rc.local Compatibility ConditionFileIsExecutable=/etc/rc.d/rc.local After=network.target After=mysqld80.service #Wants=network-online.target [Service] Type=forking ExecStart=/etc/rc.d/rc.local start TimeoutSec=0 RemainAfterExit=yes 四、rc.local例子 #!/bin/bash # THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES # # It is highly advisable to create own systemd services or udev rules # to run scripts during boot instead of using this file. # # In contrast to previous versions due to parallel execution during boot # this script will NOT be run after all other services. # # Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure # that this script will be executed during boot. touch /var/lock/subsys/local # 不推荐在rc.local启动mysql #/opt/mysql-8.0.32/bin/mysqld --defaults-file=/opt/mysql-8.0.32/my.cnf & /usr/bin/date >> /tmp/date1.log # 把当前时间追加写入到/tmp/date1.log中。 /usr/bin/sleep 15 # 睡眠15秒。 /usr/bin/date >> /tmp/date2.log # 把当前时间追加写入到/tmp/date2.log中。 # 执行sh脚本 #sh /opt/mysql-8.0.32/autoExecute.sh # mysql执行sql文件 #/opt/mysql-8.0.32/bin/mysql -h127.0.0.1 -P3306 -uroot -pMyPassword -Dmysql


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3