CentOS7如何设置开机自启动程序、开机自启动脚本?


在CentOS系统下,主要有三种方法设置自己安装的程序开机启动。

1、把启动程序的命令添加到/etc/rc.d/rc.local文件中

/etc/rc.d/rc.local 用于添加开机启动命令
/etc/rc.local/etc/rc.d/rc.local的软连接

比如下面的是设置开机启动httpd。

打开rc.local文件,在文件最后添加/usr/local/apache/bin/apachectl start

#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

#该脚本将在所有其他初始化脚本之后执行。
#如果你不想做完整的 Sys V 风格的初始化东西,你可以把你自己的初始化东西放在这里。
 
touch /var/lock/subsys/local

/usr/local/apache/bin/apachectl start

CentOS7.9里的/etc/rc.local文件

[root@localhost init.d]# cat /etc/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.

# 此文件是出于兼容性目的而添加的
#
# 强烈建议创建自己的 systemd 服务或 udev 规则以在引导期间运行脚本,而不是使用此文件。
#
# 与以前的版本相比,由于在引导期间并行执行,此脚本不会在所有其他服务之后运行。
#
# 请注意,您必须运行 'chmod +x /etc/rc.d/rc.local' 以确保此脚本将在启动期间执行。

touch /var/lock/subsys/local
[root@localhost init.d]# 

注意:/etc/rc.d/rc.local可能没有可执行权限,请检查并添加!!!

注意,这个文件可能没有可执行权限,导致centos启动时这个脚本没法启动,请用chmod 777 /etc/rc.d/rc.local给这个脚本赋权限(不要给那个软链接/etc/rc.local赋权限!!!)

参考文章:解决问题:/etc/rc.local文件配置的开机启动项不生效

注意:/etc/rc.d/rc.local可能是跟其他服务并行执行的,如果你需要在此脚本运行在某个服务启动之后才能运行的“东西”,你需要在那个“东西”上做个判断,判断所依赖的服务是否已经启动,或者添加睡眠延时,等待这个服务启动完成

2、把写好的启动脚本添加到目录/etc/rc.d/init.d/,然后使用命令chkconfig设置开机启动。

chkconfig 功能说明:检查,设置系统的各种服务。


语法:chkconfig [--add][--del][--list][系统服务] 或 chkconfig [--level <等级代号>][系统服务][on/off/reset]

--add 添加服务

--del 删除服务

--list 查看各服务启动状态

示例1:设置自启动mysql

将mysql启动脚本放入所有脚本运行目录 /etc/rc.d/init.d

cp /lamp/mysql-5.0.41/support-files/mysql.server /etc/rc.d/init.d/mysqld

改变所有者

chown root.root /etc/rc.d/init.d/mysqld

所有用户都可以执行,但只有root可以修改

chmod 755 /etc/rc.d/init.d/mysqld

将mysqld 放入linux启动管理体系中

chkconfig --add mysqld

查看全部服务在各运行级状态

chkconfig --list mysqld

只要运行级别3启动,其他都关闭

chkconfig --levels 245 mysqld off

示例2:设置自启动httpd

把httpd的脚本写好后放进/etc/rc.d/init.d/目录

chkconfig --add httpd
chkconfig httpd on

命令即设置好了开机启动。

3、把启动程序的命令添加到/etc/rc.d/rc.sysinit 文件中

脚本/etc/rc.d/rc.sysinit,完成系统服务程序启动,如系统环境变量设置、设置系统时钟、加载字体、检查加载文件系统、生成系统启动信息日志文件等

示例:设置自启动apache

echo "/usr/local/apache2/bin/apachectl start" >> /etc/rc.d/rc.sysinit

上述命令相当于在/etc/rc.d/rc.sysinit文件末尾追加/usr/local/apache2/bin/apachectl start,当然也可手动打开文件编辑

参考文章:CentOS设置程序开机自启动的方法

我们的prj

尝试方案1

我们要让/root/kyai/exec_container.sh这个脚本在centos7开机的时候自启动

先备份一下/etc/rc.d/rc.local

cp /etc/rc.d/rc.local /etc/rc.d/rc.local.bak

然后再执行

echo "/root/kyai/exec_container.sh" >> /etc/rc.d/rc.local

然后查看一下,已经写进去了

在这里插入图片描述

然后我们重启一下centos看看这个脚本能不能把服务拉起来

试了一下,我又给这个命令加了输出日志文件的功能,参考文章:Linux bash输出带日期时间的日志文件

发现这个脚本压根没运行。。。。。(不知道是不是我操作姿势有误,我是以root用户进来的)

。。。

后来发现原来是脚本没可执行权限。。。。。加上就好了,能自动运行

  • 3
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dontla

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值