phpmysqlissh2-exec

PHP calling a listener script to run continuously


I have a website that lets user select test scripts, and after submitting the desired test, it will POST to insert.php and:

  1. It inserts sends a row into MySQL database in the with done=0 (insert.php).

  2. There is a listener script (testexe.php) that I want to continuously run on the webserver (maybe sleep 10 seconds) and listen to whenever there are rows with done=0, execute them and then change done=1, and move on to the next one.

I have this script running in a while loop, however, I have a question, how can this php script initially be called? Or how can I activate it to initially start? do I have to run it like php testexe.php on the server first before submitting tests? cause I know this script just listen to events, in my case the events are done=0, but how can I keep this script running all the time (system bootup)? I don't quite get this part.

The reason I want to do it this way is because there could be multiple users sending request simultaneously, so I want to make sure they process the test one by one and queue them up.

testexe.php

<?php

...
...    

//while there are still jobs that are not done..
while (1) {
    //wait for 10 seconds and query again...
    sleep(10);
    $statusResult = mysqli_query($dbConnection, $qStatus);
    //if there are undone jobs we pick them and execute them
    if (mysqli_num_rows($statusResult) > 0){
        //query the next undone test case
        $testResult = mysqli_query($dbConnection, $qNextTest);

        //fetch the returned query object and store needed parameters 
        $row = $testResult->fetch_assoc();
        $id = $row['id'];               
        $test_script = $row['test_script']; 
        $gateway = $row['gateway'];         


        if ($connection = @ssh2_connect($gateway, 22)) {
            ssh2_auth_password($connection, $user, $password);

            //execute the test case
            $stream = ssh2_exec($connection, "/my/path/to/script.sh"); 

            //fetch output stream and stderr
            stream_set_blocking($stream, true);
            $stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
            $stream_err = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
            while($line = fgets($stream_out)) {flush(); echo '<pre>' . $line . '</pre>';}               
            echo '<pre>' . "------------------------\n" . '</pre>';
            while($line = fgets($stream_err)) {flush(); echo '<pre>' . $line . '</pre>';}
            fclose($stream);    

            //update the table after the above script is done
            $qUpdateStatus = "UPDATE table SET status = 1 WHERE id = $id";
            $updateresult = mysqli_query($dbConnection, $qUpdateStatus);
        }

    }
}

?>

Solution

  • if you use Linux cron might be something for you:

    Add this line to your crontab file (access via crontab -e)

    10   *   *   *   *   php testexe.php
    

    Then remove the while loop and sleep statement.

    If you need something else than 10 seconds this page may help you: http://www.crontabgenerator.com/