树莓派设置开机启动脚本
rc.local开机启动脚本
树莓派有多种加入自定义开机启动的方式。其中的开机启动脚本/etc/rc.local是一个比较常用的措施,它是其它服务运行之后的最后一个脚本。
使用方法
使用nano编辑内容,运行sudo nano /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) || trueif [ "$_IP" ]; then printf "My IP address is %s\n" "$_IP"fi### 在这里添加你的脚本 #### 下面一行一定要存在exit 0
编辑完成后,保存并退出。
重启服务器,查看脚本是否成功运行。
备注注意:系统在执行这段代码时使用了root用户权限,其环境变量也于树莓派普通用户pi不同。如执行脚本错误,请注意检查是否是相关原因产生。
故障排除
/etc/rc.local脚本的运行依赖 rc-local 服务。在默认情况下树莓派已经启用了 rc-local 服务。用户无须进行其它操作。如出现问题请按照以下步骤排查。
检查rc-local服务是否正常
运行sudo systemctl status rc-local,查看服务状态。此命令将输出服务的运行状态以及部分日志。
检查服务的更多日志
运行sudo journalctl -u rc-local,查看服务的更多日志。
中文翻译版以英文版相同知识授权方式共享:CC-BY-SA 4.0。交流 Q群:498908352