我的脚本允许用户使用ssh2_exec
从远程服务器执行脚本。我在MySQL中设置了一个排队机制,它使用status flag=0
执行所有服务器。我当前的代码将更改status flag=1
执行脚本,脚本完成后更改status flag=2
。但我现在我的代码只按顺序执行。意味着ssh2_exec
之后它将转移到下一个服务器。我的所有服务器都有名称,我希望所有具有不同名称的服务器同时执行脚本,有没有办法做到这一点?
testexec.php
$qStatusNotDone = "SELECT status FROM mytable WHERE status = 0"; //status = 0; not done
$rStatusNotDone = mysqli_query($dbConnection, $qStatusNotDone);
if (mysqli_num_rows($rStatusNotDone) > 0){
//for each server that is undone and about to process, we check if it is already processing and handle it accordingly...
foreach($nextTestArray as $index => $row) {
$id = $row['MIN(id)'];
$server = $row['server'];
$test_script = $row['test_script'];
echo $id . " " . $server . " " . $test_script . " ";
//if the server is already processing we skip it
if(in_array($server, $serversInProgressArray)){
echo "This server $server is doing another job, moving on to the next server\n";
}
//otherwise we run the test
else {
echo "This server $server is job free, run the test $test_script for this server!\n";
if ($connection = @ssh2_connect($server, 22)) {
ssh2_auth_password($connection, $srvrUser, $srvrPassword);
//set status to in progress and execute the test case
$qUpdateStatusInProgress = "UPDATE mytable SET status = 1 WHERE id = $id"; //status = 1; in progress
mysqli_query($dbConnection, $qUpdateStatusInProgress);
$stream = ssh2_exec($connection, "$executeScript $test_script $server $id");
//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 $line;}
echo "------------------------------------------------\n";
while($line = fgets($stream_err)) {flush(); echo $line;}
fclose($stream);
}
//update the table after the test is done
$qUpdateStatusDone = "UPDATE mytable SET status = 2 WHERE id = $id"; //status = 2; done
mysqli_query($dbConnection, $qUpdateStatusDone);
}
} //end of foreach loop
mysqli_close($dbConnection);
unset($dbConnection);
}
else {
exit("There are no jobs to process, exiting...");
}