树莓派 开机启动python脚本的一种方法
某宝上买了一个小oled屏用来在树莓派上显示一些简短信息,需要开机启动,但很久才会设置一次,记下备查:
1、写个开机启动脚本,放在/etc/init.d/下,比如:/etc/init.d/minioled
#!/bin/bash
# /etc/init.d/minioled
### BEGIN INIT INFO
# Provides: laonan
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: minioled initscript
# Description: This service is used to manage a minioled
### END INIT INFO
case "$1" in
start)
echo "Starting mini OLED"
python /home/pi/script/minioled.py &
;;
stop)
echo "Stopping mini OLED"
#killall minioled.py
kill $(ps aux | grep -m 1 'python /home/pi/script/minioled.py' | awk '{ print $2 }')
;;
*)
echo "Usage: service minioled start|stop"
exit 1
;;
esac
exit 0
2、设置开机启动
chmod +x /etc/init.d/minioled
3、然后
sudo service minioled start # 启动
sudo service minioled stop # 停止
sudo update-rc.d minioled defaults # 设置开机启动
或
sudo systemctl start minioled # 启动
sudo systemctl stop minioled # 停止
sudo systemctl restart minioled # 重启
sudo systemctl enable minioled # 设置开机启动
我更习惯用Systemd工具
参考:https://blog.csdn.net/b108074013/article/details/47443871