首页 > 技术文章 > 自定义.sh 文件在linux 中开机自启设置-九五小庞

pxyblog 2021-02-22 10:48 原文

有时候呢我们需要在centos系统启动的时候运行程序,这样保证一些服务就没有中断,相信你理解是什么意思。其实方法很简单呐,就是把需要运行的程序放在/etc/rc.d/rc.local里面就可以启动了除了常规的注册服务并设置自启动,还有一种可以设置开机自启的方式就是设置rc.local该文件位于

/etc/rc.local,

它的软链接是

/etc/rc.d/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

只有一条命令
该命令是用于管理系统时间的,不用管它

重点是自己接下来写的代码:
一般是直接添加,注意要在exit 0前面,没有就算了

例如我要开启自动执行/home 下的 time.sh 在/root目录下记录下开机时间

修改

vi /etc/rc.d/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
bash /home/time.sh

至于内容就是自由发挥了

还有最重要的一步:
在这里插入图片描述
意思是执行chmod后才能开机自启
添加权限到/etc/rc.d/rc.local
既是:

chmod +x /etc/rc.d/rc.local

推荐阅读