创建。sh文件
#! /bin/bash
touch /home/pi/test.txt
赋予权限 chmod 777 test.sh
修改 /etc/rc.local 文件
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
printf "My IP address is %s\n" "$_IP"
fi
#自己写的脚本所在的绝对目录
/home/pi/test.sh
exit 0
rc.local是否执行失败
可以通过这样来检测开机启动是否能成功,而不是每次都要重启,重启!
6 注意事项
6.1 权限
6.2 绝对路径
尽量使用绝对路径,~对应的/home/ubuntu有时会被代替为/root,从而出错
6.3 bash 代替 sh
#!/bin/sh 对应bin中的dash,版本老,有些新命令不支持,从而报错 #!/bin/bash 推荐 |
---|
6.4 rc.local中命令加权限
如下语句,如果脚本中需要sudo权限的操作会执行失败
sh /run/media/mmcblk0p4/Sen/test_start.sh & |
---|
正确如下,注意设置管理用户使用sudo命令免密模式
sudo sh /run/media/mmcblk0p4/Sen/test_start.sh & |
---|