开发者社区> 问答> 正文

配置为作为系统运行的python应用程序。d服务于树莓派。服务可以手动启动,但在启动期间不启动

我写了一个python flask的应用程序,它可以在raspberri pi上启动一个本地的web服务器。 然后我将应用程序配置为在系统引导期间作为服务启动。位于/etc/init.d中的脚本 -rwxr-xr-x 1根2359 Dez 17 18:17 VolumioEmilClient 我通过sudo update-rc启用了该服务。d VolumioEmilClient违约 之后我重新启动pi,但服务不会自动启动。 当试图在控制台手动启动服务时: 启动VolumioEmilClient 或者通过 /etc/init.d / MyService开始 服务(和python应用程序)启动良好并运行 当通过sudo systemctl手动停止服务时,停止VolumioEmilClient 奇怪的是python应用程序一直在运行(通过ps -aux检查并访问url)。 这是我的服务文件:

#!/bin/sh
### BEGIN INIT INFO
# Provides: VolumioEmilClient
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start Volumio Emil Client  at boot time
# Description:       Enable service provided by daemon.
### END INIT INFO

dir="/home/pi/volumio/python_src"
cmd="python app.py"
user="pi"

name=`basename $0`
pid_file="/var/run/$name.pid"
stdout_log="/home/pi/volumio/logs/$name.log"
stderr_log="/home/pi/volumio/logs/$name.err"

get_pid() {
    cat "$pid_file"
}

is_running() {
    [ -f "$pid_file" ] && ps -p `get_pid` > /dev/null 2>&1
}
case "$1" in
    start)
    if is_running; then
        echo "Already started"
    else
        echo "Starting $name"
        cd "$dir"
        if [ -z "$user" ]; then
            echo "-z user"
            echo $user
            sudo $cmd >> "$stdout_log" 2>> "$stderr_log" &
        else
            echo "SUDO user"
            sudo -u "$user" $cmd >> "$stdout_log" 2>> "$stderr_log" &
        fi
        echo $! > "$pid_file"
        if ! is_running; then
            echo "Unable to start, see $stdout_log and $stderr_log"
            exit 1
        fi
    fi
    ;;
    stop)
   if is_running; then
        echo -n "Stopping $name.."
        kill `get_pid`
        for i in 1 2 3 4 5 6 7 8 9 10
        # for i in `seq 10`
        do
            if ! is_running; then
                break
            fi

            echo -n "."
            sleep 1
        done
        echo
        if is_running; then
            echo "Not stopped; may still be shutting down or shutdown may have failed"
            exit 1
        else
            echo "Stopped"
            if [ -f "$pid_file" ]; then
                rm "$pid_file"
            fi
        fi
    else
        echo "Not running"
    fi
    ;;
       restart)
    $0 stop
    if is_running; then
        echo "Unable to stop, will not attempt to start"
        exit 1
    fi
    $0 start
    ;;
    status)
    if is_running; then
        echo "Running"
    else
        echo "Stopped"
        exit 1
    fi
    ;;
    *)
    echo "Usage: $0 {start|stop|restart|status}"
    exit 1
    ;;
esac

exit 0

启动后,我得到以下输出时检查状态通过 系统状态:

VolumioEmilClient.service - LSB: Start Volumio Emil Client  at boot time
   Loaded: loaded (/etc/init.d/VolumioEmilClient; generated; vendor preset: enabled)
   Active: active (exited) since Tue 2019-12-17 18:49:02 CET; 45s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 365 ExecStart=/etc/init.d/VolumioEmilClient start (code=exited, status=0/SUCCESS)
    Tasks: 0 (limit: 4915)
   CGroup: /system.slice/VolumioEmilClient.service

Dez 17 18:49:02 emil systemd[1]: Starting LSB: Start Volumio Emil Client  at boot time...
Dez 17 18:49:02 emil VolumioEmilClient[365]: Starting VolumioEmilClient
Dez 17 18:49:02 emil VolumioEmilClient[365]: SUDO user
Dez 17 18:49:02 emil systemd[1]: Started LSB: Start Volumio Emil Client  at boot time.
Dez 17 18:49:02 emil sudo[374]:     root : TTY=unknown ; PWD=/home/pi/volumio/python_src ; USER=pi ; COMMAND=/usr/bin/python app.py
Dez 17 18:49:02 emil sudo[374]: pam_unix(sudo:session): session opened for user pi by (uid=0)
Dez 17 18:49:13 emil sudo[374]: pam_unix(sudo:session): session closed for user pi

时,然后运行 " sudo systemctl start VolumioEmilClient " 我得到:

● VolumioEmilClient.service - LSB: Start Volumio Emil Client  at boot time
   Loaded: loaded (/etc/init.d/VolumioEmilClient; generated; vendor preset: enabled)
   Active: active (running) since Tue 2019-12-17 18:52:02 CET; 2s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 1042 ExecStop=/etc/init.d/VolumioEmilClient stop (code=exited, status=0/SUCCESS)
  Process: 1058 ExecStart=/etc/init.d/VolumioEmilClient start (code=exited, status=0/SUCCESS)
    Tasks: 2 (limit: 4915)
   CGroup: /system.slice/VolumioEmilClient.service
           ├─1063 sudo -u pi python app.py
           └─1070 python app.py

Dez 17 18:52:02 emil systemd[1]: Starting LSB: Start Volumio Emil Client  at boot time...
Dez 17 18:52:02 emil VolumioEmilClient[1058]: Starting VolumioEmilClient
Dez 17 18:52:02 emil VolumioEmilClient[1058]: SUDO user
Dez 17 18:52:02 emil sudo[1063]:     root : TTY=unknown ; PWD=/home/pi/volumio/python_src ; USER=pi ; COMMAND=/usr/bin/python app.py
Dez 17 18:52:02 emil sudo[1063]: pam_unix(sudo:session): session opened for user pi by (uid=0)
Dez 17 18:52:02 emil systemd[1]: Started LSB: Start Volumio Emil Client  at boot time.

这里我可以看到python应用程序已经启动。为什么不在开机? 问题来源StackOverflow 地址:/questions/59379647/python-app-configured-to-run-as-system-d-service-on-raspberry-pi-service-can-be

展开
收起
kun坤 2019-12-29 21:55:05 867 0
1 条回答
写回答
取消 提交回答
  • 可能没有按照相应的依赖或环境

    2020-03-25 16:44:01
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载