[英]RPi running a GPIO script on boot
我有一个脚本需要在Raspberry Pi引导时运行(Raspbian最新版本,Pi是B +型)。 该脚本必须是无阻塞的,并且访问GPIO引脚,因此需要以root身份运行。 它也是Python3。
我试图将脚本设置为服务,并将其放在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的东西通常是)。
关于我可以尝试的任何想法?
该服务是按照以下说明进行设置的: 在启动时让Python脚本在后台(作为服务)运行 。 我在Linux上的表现不是很好(已经使用了多年……但是我从来没有将焊锡焊接到PC上……)
另外,在Pi启动时,有关如何运行脚本的更好的想法需要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
这还将具有不断检查脚本是否正在运行的优点,并在需要时重新启动它。
我希望这会有所帮助,再见。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.