Python守护进程使用initscript正确启动,但在启动时失败

2024-09-29 21:46:14 发布

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

我有一些python守护进程,在启动时用init脚本运行raspberry pi应用程序。在

init脚本从控制台运行良好,可以正确启动和结束后台进程。在

脚本是用sudo insserv GartenwasserUI编写的autostart

它在启动时启动,这可以通过打开LCD背光来证明,但登录后不在进程列表中。使用sudo service GartenwasserUI start手动启动可立即工作。在

怎么了?在

这是剧本

#!/bin/sh

### BEGIN INIT INFO
# Provides:          GartenwasserUI
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: GartenwasserUI acts as a mqtt client for LCD displaying and switching gpio through mqtt
# 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=/usr/local/bin/Gartenwasser
DAEMON=$DIR/GartenwasserUI.py
DAEMON_NAME=GartenwasserUI

# 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"
    cd $DIR
    #python ./$DAEMON_NAME.py &
    start-stop-daemon --start --background --pidfile $PIDFILE --make-pidfile --user $DAEMON_USER --chuid $DAEMON_USER --startas $DAEMON -- $DAEMON_OPTS  --verbose -stdout /var/log/GartenwasserUI.log
    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

剧本本身:

^{pr2}$

Tags: ofthename脚本log进程initexit
1条回答
网友
1楼 · 发布于 2024-09-29 21:46:14

我找到了解决办法: 当机器启动时,mosquitto服务用S02启动。由于python守护进程init脚本没有依赖于mosquitto的信息,所以它们也以S02开始。

解决问题的改变:

在init脚本的LSB头中设置依赖项:

# Required-Start:    $remote_fs $syslog mosquitto

之后,两个python守护进程都作为S03正确执行。

相关问题 更多 >

    热门问题