RPI运行GPIO脚本在启动
我有一个需要运行的时候树莓派靴(Raspbian-最新版本中,PI是模型B +)的脚本。脚本需要非阻塞,并访问GPIO引脚,因此需要以root用户身份运行。这也是Python3。RPI运行GPIO脚本在启动
我试图设置脚本了作为一种服务,并把它放在init.d
在引导运行。
这是我的服务:
#!/bin/sh
### BEGIN INIT INFO
# Provides: myservice
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Put a short description of the service here
# Description: Put a long description of the service here
### END INIT INFO
# Change the next 3 lines to suit where you install your script and what you want to call it
DIR=/home/pi
DAEMON=$DIR/server2.py
DAEMON_NAME=bottleserver
# Add any command line options for your daemon here
DAEMON_OPTS=""
# This next line determines what user the script runs as.
# Root generally not recommended but necessary if you are using the Raspberry Pi GPIO from Python.
DAEMON_USER=root
# The process ID of the script when it runs is stored here:
PIDFILE=/var/run/$DAEMON_NAME.pid
. /lib/lsb/init-functions
do_start() {
log_daemon_msg "Starting system $DAEMON_NAME daemon"
start-stop-daemon --start --background --pidfile $PIDFILE --make-pidfile --user $DAEMON_USER --chuid $DAEMON_USER --startas $DAEMON -- $DAEMON_OPTS
log_end_msg $?
}
do_stop() {
log_daemon_msg "Stopping system $DAEMON_NAME daemon"
start-stop-daemon --stop --pidfile $PIDFILE --retry 10
log_end_msg $?
}
case "$1" in
start|stop)
do_${1}
;;
restart|reload|force-reload)
do_stop
do_start
;;
status)
status_of_proc "$DAEMON_NAME" "$DAEMON" && exit 0 || exit $?
;;
*)
echo "Usage: /etc/init.d/$DAEMON_NAME {start|stop|restart|status}"
exit 1
;;
esac
exit 0
我可以通过键入
sudo /etc/init.d/bottleserver.sh start
我已经在努力建立链接,这样事情办得做
sudo update-rc.d bottleservice.sh defaults
运行在开机时。如果我查看这些链接的状态,我会得到:
ls -l /etc/rc?.d/*bottleserver.sh
lrwxrwxrwx 1 root root 25 Oct 2 20:56 /etc/rc0.d/K01bottleserver.sh -> ../init.d/bottleserver.sh
lrwxrwxrwx 1 root root 25 Oct 2 20:56 /etc/rc1.d/K01bottleserver.sh -> ../init.d/bottleserver.sh
lrwxrwxrwx 1 root root 25 Oct 2 20:56 /etc/rc2.d/S02bottleserver.sh -> ../init.d/bottleserver.sh
lrwxrwxrwx 1 root root 25 Oct 2 20:56 /etc/rc3.d/S02bottleserver.sh -> ../init.d/bottleserver.sh
lrwxrwxrwx 1 root root 25 Oct 2 20:56 /etc/rc4.d/S02bottleserver.sh -> ../init.d/bottleserver.sh
lrwxrwxrwx 1 root root 25 Oct 2 20:56 /etc/rc5.d/S02bottleserver.sh -> ../init.d/bottleserver.sh
lrwxrwxrwx 1 root root 25 Oct 2 20:56 /etc/rc6.d/K01bottleserver.sh -> ../init.d/bottleserver.sh
所以一些肯定存在。但是它不会在启动时启动。我没有得到任何错误(脚本本身 - server2.py
,有错误记录到文件),但它也不会运行。我认为这是与权限有关? (GPIO的东西通常是)。
什么我可以尝试任何想法?
服务成立了以下说明:Getting a Python script to run in the background (as a service) on boot。我不是真的在Linux上很好(已经使用了多年..但我从来没有位焊接到PC ...)
另外,就如何当运行一个脚本需要GPIO访问更好的想法皮靴?
我不确定这个“瓶服务器”实际上会为你做什么,但是如果它需要做一些需要工作网络连接的事情,那么它有可能启动,然后它突然退出并显示错误状态。这可能是由于启动时无法找到活动连接,这也可以解释为什么,在几分钟后,您可以手动成功启动它。
这正是我在使用ngrok(实际上我正在寻找更多的一些信息,当我发现你的问题)时发生的情况。
我没有使用“服务”方法,而是开始使用/etc/wicd/wired-settings.conf
和/etc/wicd/wireless-settings.conf
的afterscript = $PATH_TO_YOUR_SCRIPT
部分(我正在使用wicd来管理网络)。在使用此解决方案之前,我尝试将后缀脚本添加到/etc/network/interfaces
。这是一样的:无论如何,我总是需要大约一分钟的延迟(仅使用sleep 60
)来启动依赖于网络的任何脚本,即使它们是在所选网络管理器连接后执行的。要获得第二个替代方案,我发现它更可靠,可以切换回crontab
,安排脚本每隔X分钟执行一次(比如说*/3 * * * * $PATH_TO_YOUR_SCRIPT
),并在脚本中添加一个区域,检查它是否已在运行。例如:
pidof $YOUR_COMMAND # returns nothing if no PID exists
if (($?)); then
# Failure
# rest of your script, launch your network stuff
else
# Success, NOTHING TO DO
fi
exit 0
这也将有一个连续检查,如果它正在运行的脚本,需要时重新启动它的优势。
我希望这有助于,再见。