首页 > 解决方案 > php -> 定时 bash 脚本总是关闭

问题描述

我有这个愚蠢但不完美但可以工作的 Bash 脚本

#!/bin/bash

    scriptstart="script"
    scriptstop="script2"
    LogTime=$(date '+[%d.%m.%Y %H:%M:%S]');
    Time=$(date '+[%H:%M]');
    starttimehc="06:00"
    stoptimehc="07:30"
    starttime2hc="18:30"
    stoptime2hc="21:30"
    starttime="$2"
    stoptime="$3"
    starttime2="$4"
    stoptime2="$5"
    startnow="$1"

    if [ -z "$starttime" ]; then
        starttime="$starttimehc"
    fi

    if [ -z "$stoptime" ]; then
        stoptime="$stoptimehc"
    fi

    if [ -z "$starttime2" ]; then
        starttime2="$starttime2hc"
    fi

    if [ -z "$stoptime2" ]; then
        stoptime2="$stoptime2hc"
    fi

    echo $starttime : $stoptime : $starttime2 : $stoptime2

    trap ctrl_c INT

    function Log {
                        echo "$LogTime" " - " "$1" >> /home/pi/Desktop/Lightshow/Logs/TimedShowLog_$(date '+%d.%m.%Y')
            echo "$LogTime" " - " "$1"
                }

    function Start {
            # run target script
            Log " Lightshow Start at $(date '+%H:%M') "
                . $scriptstart
            WaitforStop
            $SHELL
        }

    function Stop {
            #Stop Lights
            Log "Lightshow Stopped at $(date '+%H:%M')"
            . $scriptstop;
            WaitforStart
        }

    function WaitforStart {
            Log "Waiting For the next time to Start"
            while [ $(date '+%H:%M') != $starttime ] || [ $(date '+%H:%M') != $starttime2 ]; do
                sleep 10;
            done;
            Start
        }

    function WaitforStop {
            Log "Waiting For the next time to Stop"
            while [ $(date '+%H:%M') != $stoptime ] || [ $(date '+%H:%M') != $stoptime2 ]; do
            Log "Still Allive"
                sleep 10;
            done;
            Stop
        }

    function ctrl_c() {
            Log "Lightshow Closed by User at $(date '+%H:%M')"
                . $scriptstop;
            exit
        }
    # Starting Point
    if [ -z "$startnow" ]; then
        echo "Start Lights Now Or Timed?"
        select yn in "Now" "Timed"; do
            case $yn in
                Now ) Start;;
                Timed ) WaitforStart;;
            esac
        done
    elif [ "$startnow" == "1" ]; then
        Start
    elif [ "$startnow" == "0" ]; then
        WaitforStart
    fi

这永远不会关闭,因为它会在执行其他脚本时执行,当我在终端中启动它时脚本工作正常。但由于我想控制一个网站(不同的时间/开始/停止),我需要通过 php 执行它。我的PHP代码:

 <?php
    $name = $_POST['name'];
    ini_set('max_execution_time', 0);
    switch ($name) {
        case "starttimeddata":
        $starttime1 = $_POST['starttime1'];
        $stoptime1 = $_POST['stoptime1'];
        $starttime2 = $_POST['starttime2'];
        $stoptime2 = $_POST['stoptime2'];
          echo "First Start : " . $starttime1 . "<br>Stop at : " . $stoptime1 . "<br><hr><br>Second Start : " . $starttime2 . "<br>Stop at :  " . $stoptime2 . "";
          shell_exec("bash /var/www/html/microweb/cgi-bin/result/bash/Start.sh 1 " . $starttime1 . " " . $stoptime1 . " " . $starttime2 . " " . $stoptime2 . "");
            break;
        default:
            echo "something wrong";
    }
    ?>

这只是一个不好的方法吗?甚至可以运行一个永远不会关闭的 bash 脚本吗?

因为现在脚本开始但在 5 秒后关闭我也尝试使用 nohub 但结果相同

在树莓派上运行 apache2

标签: phplinuxbashcgiraspberry-pi3

解决方案


推荐阅读