初始化脚本在启动时不工作,在其他情况下可以正常工作

2024-04-26 19:15:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我基于双叉方法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

我已经将重定向到/tmp中文件的echo语句放入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

Tags: ofthename脚本loghereinitexit