单独为一个用户设置开机自启。
-
新建一个目录用于存放开机启动脚本
mkdir ~/auto_start
-
编辑
/etc/rc.local
文件 在exit 0
前面输入如下内容filepath="/home/pi/auto_start/" # /home/pi/auto_start/ 代表本机中需要开机自启动的脚本目录 files=`ls ${filepath}` for file in ${files[*]} do # 该文件具有可执行权限 if [ -x "${filepath}${file}" ]; then su - ${user} -c ${filepath}${file} fi done
-
在脚本目录中新建脚本 (要有可执行权限) 后每次开机将会执行这个目录下的所有脚本
extends
为所有用户添加开机启动
all_user=`cat /etc/passwd | grep "/bin/bash" | awk -F: '{print $1}'`
for user in ${all_user[*]}
do
# 用户家目录 + /auto_start/
filepath=`cat /etc/passwd | grep /bin/bash | grep "${user}:" | awk -F: {print'$6'}`/auto_start/
#filepath="/home/${user}/auto_start/" 有局限性。
# 如果存在该目录
if [ -d "$filepath" ]; then
files=`ls ${filepath}`
for file in ${files[*]}
do
# 该文件具有可执行权限
if [ -x "${filepath}${file}" ]; then
su - ${user} -c ${filepath}${file}
fi
done
else
su - ${user} -c "mkdir -p ${filepath}"
su - ${user} -c "echo '该目录下的所有可执行文件将会在开机时自动执行。(文件需要有可执行权限。 )' > ${filepath}README.txt"
fi
done
这样设置后所有在系统中的用户只要在家目录中创建auto_start
文件夹
在开机时将会执行该文件夹下所有脚本。