Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 启动时运行GPIO脚本的RPi_Python_Linux_Raspberry Pi_Raspbian_Gpio - Fatal编程技术网

Python 启动时运行GPIO脚本的RPi

Python 启动时运行GPIO脚本的RPi,python,linux,raspberry-pi,raspbian,gpio,Python,Linux,Raspberry Pi,Raspbian,Gpio,我有一个脚本需要在Raspberry Pi启动时运行(Raspbian-最新版本,Pi是型号B+)。该脚本需要无阻塞,并访问GPIO引脚,因此需要以root用户身份运行。它也是蟒蛇3 我尝试将脚本设置为服务,并将其放入init.d以在启动时运行 这是我的服务: #!/bin/sh ### BEGIN INIT INFO # Provides: myservice # Required-Start: $remote_fs $syslog # Required-Stop:

我有一个脚本需要在Raspberry Pi启动时运行(Raspbian-最新版本,Pi是型号B+)。该脚本需要无阻塞,并访问GPIO引脚,因此需要以root用户身份运行。它也是蟒蛇3

我尝试将脚本设置为服务,并将其放入
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内容通常是)

我能试试什么吗

服务是按照以下说明设置的:。我对Linux不是很在行(我已经用了很多年了……但我从来没有把一些东西焊接到电脑上……)


或者,更好地了解如何在Pi启动时运行需要GPIO访问的脚本?

我不确定这个“瓶装服务器”究竟为您做了什么,但如果它需要做一些需要工作网络连接的事情,它可能会启动,然后突然以错误状态退出。这可能是因为启动时找不到活动连接,这也解释了为什么几分钟后,您可以手动成功启动它

这正是我在使用时遇到的情况(事实上,当我发现您的问题时,我正在搜索更多信息)

我开始使用
/etc/wicd/wired settings.conf
/etc/wicd/wireless settings.conf
(我正在使用wicd管理网络)的
afterscript=$PATH\u TO\u YOUR\u SCRIPT
部分,而不是使用“服务”方法。在使用此解决方案之前,我尝试在
/etc/network/interfaces
中添加一个post-up脚本。这是一样的:我发现无论如何,在启动依赖于网络的任何脚本时,我总是需要大约一分钟的延迟(只需使用
sleep 60
),即使它们是在所选网络管理器连接后执行的

为了有第二种选择,我觉得更可靠,您可以切换回
crontab
,计划每X分钟执行一次脚本(比如
*/3****$PATH\u To\u您的脚本
),并在脚本中添加一个部分,检查脚本是否已经运行。比如:

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
这还具有持续检查脚本是否正在运行的优点,可以在需要时重新启动脚本

我希望这有帮助,再见