我根据双叉方法here写了一个python守护进程。
当使用start | stop参数直接调用scsdaemon.py
时,它完全有效,并且当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
我已经将一个echo语句重定向到/tmp
中的文件到init脚本中,因此可以验证脚本是否已运行。
但服务没有开始。日志中没有单个消息,我的应用程序创建的日志也是空的。
我错过了什么?为什么这在正常运行期间有效但在启动期间无效?
#!/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