我在这里基于双叉方法编写了一个 python 守护程序。
它在scsdaemon.py
使用 start|stop 参数直接调用时非常有效,并且在 raspberry 已经启动时调用下面的 init 脚本也可以正常工作。
我确保在启动期间通过运行调用脚本,sudo update-rc.d scsdaemon defaults
并确保它是可执行的:
$ ls -l /etc/init.d/scsdaemon
-rwxr-xr-x 1 root root 1639 Mar 14 19:25 /etc/init.d/scsdaemon
我已经在 init 脚本中放入了一个重定向到文件的 echo 语句,/tmp
因此可以验证脚本是否运行。
但是服务没有启动。日志中没有一条消息,我的应用程序创建的日志也在那里但为空。
我错过了什么?为什么这在正常运行期间有效,但在启动期间无效?
#!/bin/sh
### BEGIN INIT INFO
# Provides: scsdaemon
# Required-Start: $remote_fs $syslog $network
# Required-Stop: $remote_fs $syslog $network
# 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=/var/www/bin/py
DAEMON=$DIR/scsdaemon.py
DAEMON_NAME=scsdaemon
# 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=www-data
# The process ID of the script when it runs is stored here:
PIDFILE=/tmp/$DAEMON_NAME.pid
. /lib/lsb/init-functions
do_start () {
log_daemon_msg "Starting system $DAEMON_NAME daemon"
start-stop-daemon --start --user $DAEMON_USER --chuid $DAEMON_USER --exec $DAEMON -- start $DAEMON_OPTS
log_end_msg $?
}
do_stop () {
log_daemon_msg "Stopping system $DAEMON_NAME daemon"
start-stop-daemon --start --exec $DAEMON --retry 10 -- stop $DAEMON_OPTS
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