自定义.sh 文件在linux 中开机自启设置-九五小庞

有时候呢我们需要在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
posted @   九五小庞  阅读(628)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示