重启后脚本并没有自动启动。
1. 创建风扇控制脚本
cat > /etc/init.d/fan_auto <<'EOF'
#!/bin/sh
### BEGIN INIT INFO
# Provides: fan_auto
# Required-Start: $local_fs $remote_fs $syslog
# Required-Stop: $local_fs $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Fan auto control for SGW-G2
# Description: Smart fan control based on CPU and disk temp
### END INIT INFO
SCRIPT="/root/fan_auto.sh"
PIDFILE="/var/run/fan_auto.pid"
start() {
if [ -f "$PIDFILE" ] && kill -0 "$(cat $PIDFILE)" 2>/dev/null; then
echo "fan_auto already running"
return 1
fi
echo "Starting fan_auto control..."
nohup $SCRIPT > /dev/null 2>&1 &
echo $! > "$PIDFILE"
}
stop() {
if [ -f "$PIDFILE" ]; then
kill "$(cat $PIDFILE)" 2>/dev/null
rm -f "$PIDFILE"
echo "fan_auto stopped"
else
echo "fan_auto not running"
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 2
start
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
;;
esac
EOF
2. 赋予执行权限
chmod +x /etc/init.d/fan_auto
3. 注册为系统服务(关键!)
update-rc.d fan_auto defaults
✅ 这条命令会在 /etc/rc[0-6].d/ 下创建符号链接,确保开机/关机时自动调用。
4. 手动启动测试
service fan_auto start
# 或
/etc/init.d/fan_auto start
5. 验证进程
ps aux | grep fan_auto.sh
tail /var/log/fan_auto.log
🔄 重启验证
重启后登录,检查:
ps aux | grep fan_auto.sh # 应在运行
service fan_auto status # 可能不支持 status,但 start/stop 有效
❓为什么这次能持久生效?
- 飞牛的根文件系统虽然是只读的,但
/etc 是 overlay 上层可写部分(因为你在里面看到了 rc.local 等可修改文件)
update-rc.d 创建的链接和脚本会保存在可写层
- 系统启动时会按标准 SysV 流程执行
/etc/init.d/ 脚本
💡 你之前能修改 /etc/rc.local 并保存,就证明 /etc 是可持久化的!
🛠️ 后续管理命令
# 启动
service fan_auto start
# 停止
service fan_auto stop
# 重启
service fan_auto restart
# 取消自启(如需)
update-rc.d -f fan_auto remove