在 linux 中基于 supervisor 开机自启的python程序
排版不是很好 见谅
先下载:
sudo apt-get install supervisor
[inet_http_server]
port=*:9001 ;Web管理后台运行的IP和端口,建议监听内网ip
要求:在树莓派中写好程序,在树莓派开机是自动运行python程序
上图中:music_serve.py 和 music_client.py 是需要在树莓派开机时自启的 python程序。
首先:需要进入根目录下 cd /etc/supervisor/conf.d
在 conf.d 中创建 以 .conf 结尾的文件 (文件名任意),
之后打开 创建的 .conf 文件 : 并在文件中填入以下内容:
[program:Smart_Home]
user=xiaoshi
# 用户名
directory=/home/xiaoshi/project/music-light/music_files
command=nohup /usr/bin/python3 music_server.py
# /usr/bin/python3:为用到的python路径,music_server.py为要自启时运行的 py 程序文件
autostart=true # 默认值
autorestart=true # 默认值
startretries=3 # 默认值
startsecs=20 # 默认值
配置好如下:
注意:一个python程序需要一个 .conf 文件,需要多个时 创建多个 .conf 文件
所有的 .conf 文件 都必须放在 /etc/supervisor/conf.d 文件夹中
配置完成后:命令重新加载:
sudo systemctl restart supervisor
sudo supervisorctl status
访问该树莓派的 IP 地址 端口配置为 9001 如下结果 表示成功: