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

20 篇文章 1 订阅

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

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 </opt/mysql-8.0.32/autoExecute.sql

systemd service 配置自启动,配置多个环境变量,最大打开文件数

五、参考

systemd启动流程分析

Systemd-服务启动顺序

  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
rc.local的加载顺序可以通过修改系统的启动配置文件来调整。在引用中给出的示例中,rc.local文件中的内容会在每个多用户运行级别结束时执行。这意味着在系统启动过程中,会先执行其他启动脚本和服务,然后才执行rc.local文件中的内容。 如果你想要调整rc.local的加载顺序,你可以修改系统的启动配置文件。例如,在引用中给出的示例中,可以使用systemd来控制rc.local的加载顺序。在这个示例中,redis6.service是一个systemd服务,可以通过修改该服务的配置文件来控制rc.local的加载顺序。 另外,需要注意的是,当在rc.local文件中执行脚本时,可能需要先让环境变量生效。可以通过在rc.local文件中添加". /etc/profile"或"source /etc/profile"命令来实现。这样做可以确保在执行rc.local中的脚本时,环境变量已经生效了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Linux 配置文件 启动文件rc.local、/etc/bash.bashrc、~/bashrc、/etc/profile、~/.profile加载顺序](https://blog.csdn.net/juxua_xatu/article/details/21289789)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [systemd servicerc.local查看启动顺序调整启动顺序](https://blog.csdn.net/haveqing/article/details/130343394)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [/etc/profile, rc.local等文件的执行顺序](https://blog.csdn.net/chao821/article/details/108674492)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值