微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.

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

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用户进来的)

原文地址:https://www.jb51.cc/wenti/3282204.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐