自定义.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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .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 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义