使用PHP和MySQL按顺序执行脚本

时间:2017-09-08 16:44:04

标签: php mysql bash mysqli ssh2-exec

我有一个网站,允许用户从单选按钮中选择测试脚本,并在使用PHP将所需测试提交到远程服务器之后:

  1. 它在test_activity_log表中插入一行到MySQL数据库,包含id,username,test name,gateway name和timestamp。
  2. 然后发送ssh2_exec以在远程网关上执行所需的脚本
  3. 上面的所有内容我已经有了工作,但是,当多个用户同时从网页执行脚本时,我需要找到一种方法来按顺序排列它们并一个接一个地执行它们。最好的方法是什么?

    非常感谢任何想法。

    编辑:

    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);
            }
    
        }
    }
    
    ?>
    

0 个答案:

没有答案