linux 服务自启动

实操步骤(共 3 步)
步骤 1:确保 rc.local 文件可执行(部分系统默认禁用)
bash
运行

赋予执行权限

sudo chmod +x /etc/rc.local

确保文件有执行入口(首行必须是#!/bin/sh -e)

sudo vim /etc/rc.local
文件首行必须是:#!/bin/sh -e(-e表示脚本执行出错时停止,避免后续命令异常)。
步骤 2:在 rc.local 中添加脚本执行命令
在文件 **exit 0之前 ** 添加你的脚本绝对路径(关键!exit 0之后的内容不会执行):
bash
运行

!/bin/sh -e

其他系统默认命令...

【添加这一行】你的脚本绝对路径,后台运行加&(避免阻塞系统启动)

/usr/local/bin/auto_run.sh &

exit 0
注意:加&表示后台运行,若脚本是前台阻塞型(如持续监控),不加&会导致系统启动卡住。
步骤 3:验证配置
bash
运行

重新加载rc.local配置

sudo systemctl restart rc-local

测试执行

/etc/rc.local

posted on 2026-02-03 14:39  张博的博客  阅读(2)  评论(0)    收藏  举报

导航