2014-10-04 153 views
0

我有一個需要運行的時候樹莓派靴(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訪問更好的想法皮靴?

回答

1

我不確定這個「瓶服務器」實際上會爲你做什麼,但是如果它需要做一些需要工作網絡連接的事情,那麼它有可能啓動,然後它突然退出並顯示錯誤狀態。這可能是由於啓動時無法找到活動連接,這也可以解釋爲什麼,在幾分鐘後,您可以手動成功啓動它。

這正是我在使用ngrok(實際上我正在尋找更多的一些信息,當我發現你的問題)時發生的情況。

我沒有使用「服務」方法,而是開始使用/etc/wicd/wired-settings.conf/etc/wicd/wireless-settings.confafterscript = $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 

這也將有一個連續檢查,如果它正在運行的腳本,需要時重新啓動它的優勢。

我希望這有助於,再見。