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