简体   繁体   English

RPi在启动时运行GPIO脚本

[英]RPi running a GPIO script on boot

I have a script that needs to run when the Raspberry Pi boots (Raspbian- latest version, the Pi is a model B+). 我有一个脚本需要在Raspberry Pi引导时运行(Raspbian最新版本,Pi是B +型)。 The script needs to be none-blocking, and accesses the GPIO pins, so needs to run as root. 该脚本必须是无阻塞的,并且访问GPIO引脚,因此需要以root身份运行。 It's also Python3. 它也是Python3。

I have tried to set the script up as a service, and put it in init.d to run on boot. 我试图将脚本设置为服务,并将其放在init.d以在引导时运行。

This is my service: 这是我的服务:

#!/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

I can run it by typing 我可以通过键入来运行它

sudo /etc/init.d/bottleserver.sh start

I have done 我已经做好了

sudo update-rc.d bottleservice.sh defaults

in an effort to set up the links so things run on boot. 为了建立链接,使事情在启动时运行。 If I check the status of these links I get: 如果我检查这些链接的状态,则会得到:

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

so some definitely exist. 所以一定存在。 However it doesn't start on boot. 但是,它不会在启动时启动。 I don't get any errors (the script itself- server2.py , has error logging to a file), but it also doesn't run. 我没有收到任何错误(脚本本身server2.py ,将错误记录到文件中),但是它也没有运行。 I assume it is something to do with permissions? 我认为这与权限有关? (GPIO stuff usually is). (GPIO的东西通常是)。

Any ideas on what I can try? 关于我可以尝试的任何想法?

The service was set up following these instructions: Getting a Python script to run in the background (as a service) on boot . 该服务是按照以下说明进行设置的: 在启动时让Python脚本在后台(作为服务)运行 I'm not really very good on Linux (have used it for years.. but I never had to solder bits to the PC...) 我在Linux上的表现不是很好(已经使用了多年……但是我从来没有将焊锡焊接到PC上……)

Alternatively, better ideas on how to run a script needed GPIO access when the Pi boots? 另外,在Pi启动时,有关如何运行脚本的更好的想法需要GPIO访问?

I'm not sure about what this "bottleserver" actually does for you, but if it needs to do something requiring a working network connection, there are possibilities that it starts and then it suddenly exits with an error status. 我不确定这个“瓶子服务器”实际上为您做了什么,但是如果它需要做一些需要有效的网络连接的操作,则有可能启动它,然后突然退出并显示错误状态。 This can be due to the fact that on startup it can't find the active connection and this would explain also why, a few moments after, you can successfully start it manually. 这可能是由于在启动时找不到活动的连接,这也可以解释为什么稍后可以手动成功启动它。

This is exactly the case I encountered while using ngrok (and actually I was searching for some more info when I found your question). 这正是我在使用ngrok时遇到的情况(实际上,当我找到您的问题时,我正在搜索一些更多信息)。

Instead of using the "service" approach, I started using the afterscript = $PATH_TO_YOUR_SCRIPT section of /etc/wicd/wired-settings.conf and /etc/wicd/wireless-settings.conf (I'm using wicd to manage networks). 我没有使用“服务”方法,而是开始使用/etc/wicd/wired-settings.conf/etc/wicd/wireless-settings.confafterscript = $PATH_TO_YOUR_SCRIPT部分(我正在使用wicd管理网络) 。 Before using this solution, I tried adding a post-up script to /etc/network/interfaces . 在使用此解决方案之前,我尝试将后置脚本添加到/etc/network/interfaces It's the same: I found anyway that I always need about one minute of delay (using simply a sleep 60 ) in launching any script that relies on the network, even if they are executed after the connection by the chosen network manager. 是一样的:无论如何,我发现启动依赖于网络的任何脚本时总是需要大约一分钟的延迟(仅使用sleep 60 ),即使它们是由选定的网络管理员在连接后执行的。

To have a second alternative, that I find more reliable, you could switch back to crontab , scheduling your script to be executed every X minutes (say */3 * * * * $PATH_TO_YOUR_SCRIPT ) and adding to your script a section where you check if it's already running. 作为第二种选择,我认为它更可靠,您可以切换回crontab ,安排脚本每X分钟执行一次(例如*/3 * * * * $PATH_TO_YOUR_SCRIPT ),然后在脚本中添加一个您检查的部分如果它已经在运行。 Something like: 就像是:

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

This will also have the advantage of a continuous check if the script it's running, relaunching it when needed. 这还将具有不断检查脚本是否正在运行的优点,并在需要时重新启动它。

I hope this helps, bye. 我希望这会有所帮助,再见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM