准备工作

普通用户ks加入了sudoer ,免密
用ks登录系统

开启 rc-local服务

sudo systemctl enable rc-local

修改rc.local

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

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

su - ks -s "/ks/start_bfs_all.sh"

每次系统启动都会toch /var/lock/subsys/local ,这个文件的修改时间也就是系统最后启动时间,后面我们加了一行我们要启动脚本 。

  • - ks表示要切换到ks用户下执行
  • -s表示要执行的脚本文件,要用绝对路径

执行的脚本要有执行权限

sudo  chmod +x /ks/start_bfs_all.sh

示例: start_bfs_all.sh脚本内容如下,这里脚本又嵌套了脚本

cd /aos/ks/9324 && "./start.sh"
cd /aos/ks/afs_bin &&"./start.sh"
cd /ks/9210ED00-09 && "./start.sh"
cd /ks/9324FF00-39 && "./start.sh"
cd /ks/pn/pnc && "./start.sh

测试自启动:启动rc-local或者重启系统

su root
sudo  systemctl start rc-local  

or

sudo  systemctl reboot

检查你的程序是否启动

问题排查: 脚本执行出错?

journalctl -e -u rc-local.service
systemctl status rc-local
sudo cat /var/log/syslog | grep -i rc.local