树莓派设置开机启动

发布时间 2023-08-01 16:32:29作者: 虎虎生威啊

树莓派设置开机启动

前言

树莓派设置开机自启,看了好多教程都没有成功.
我是要设置树莓派开机运行一个 fastapi 或者 flask 的服务,要求该程序在后台运行

创建一个 bash 脚本运行 python 程序

  1. 很关键,一定要使用 bash 脚本去运行 python 程序,然后关键一步要给你的 bash 脚本
    sudo chmod 777 <你的脚本>

设置 /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
# 所有要执行的脚本,都放在这里,一行一个
su pi -c "exec /home/pi/Desktop/pushrod/backend/1.sh &"

exit 0

几点说明

  1. 末尾的 & ,用来表示程序在后台运行
  2. 前面都是固定的格式
su pi -c "exec <你的脚本的位置.sh> &"