PHP 连续调用侦听器脚本 运行
PHP calling a listener script to run continuously
我有一个允许用户 select 测试脚本的网站,在提交所需的测试后,它将 POST 到 insert.php 和:
它在 with done=0 (insert.php) 中向 MySQL 数据库插入一行。
有一个侦听器脚本 (testexe.php),我想在网络服务器上连续 运行(可能会睡 10 秒)并在有完成的行时收听=0,执行它们,然后更改done=1,继续进行下一个。
我在 while 循环中有这个脚本 运行ning,但是,我有一个问题,这个 php 脚本最初是如何调用的?或者我怎样才能激活它来初始启动?在提交测试之前,我是否必须先在服务器上 运行 像 php testexe.php
一样?因为我知道这个脚本只是监听事件,在我的例子中事件是 done=0,但是我怎样才能让这个脚本一直保持 运行ning(系统启动)?我不太明白这部分。
之所以要这样做是因为可能有多个用户同时发送请求,所以我想确保他们一个一个地处理测试并排队。
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);
}
}
}
?>
我可以想到两个选项:(1) 在您的网络服务器上创建一个 cron 作业,每 ~5 分钟调用一次 testexe.php,或者 (2) 让 POST 执行 testexe.php 脚本,注意 testexe.php 检查 testexe.php 的另一个实例当前是否为 运行.
如果您使用 Linux cron
可能适合您:
将此行添加到您的 crontab 文件(通过 crontab -e
访问)
10 * * * * php testexe.php
然后删除 while 循环和 sleep 语句。
如果您需要 10 秒以外的其他内容,此页面可能对您有所帮助:http://www.crontabgenerator.com/
我有一个允许用户 select 测试脚本的网站,在提交所需的测试后,它将 POST 到 insert.php 和:
它在 with done=0 (insert.php) 中向 MySQL 数据库插入一行。
有一个侦听器脚本 (testexe.php),我想在网络服务器上连续 运行(可能会睡 10 秒)并在有完成的行时收听=0,执行它们,然后更改done=1,继续进行下一个。
我在 while 循环中有这个脚本 运行ning,但是,我有一个问题,这个 php 脚本最初是如何调用的?或者我怎样才能激活它来初始启动?在提交测试之前,我是否必须先在服务器上 运行 像 php testexe.php
一样?因为我知道这个脚本只是监听事件,在我的例子中事件是 done=0,但是我怎样才能让这个脚本一直保持 运行ning(系统启动)?我不太明白这部分。
之所以要这样做是因为可能有多个用户同时发送请求,所以我想确保他们一个一个地处理测试并排队。
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);
}
}
}
?>
我可以想到两个选项:(1) 在您的网络服务器上创建一个 cron 作业,每 ~5 分钟调用一次 testexe.php,或者 (2) 让 POST 执行 testexe.php 脚本,注意 testexe.php 检查 testexe.php 的另一个实例当前是否为 运行.
如果您使用 Linux cron
可能适合您:
将此行添加到您的 crontab 文件(通过 crontab -e
访问)
10 * * * * php testexe.php
然后删除 while 循环和 sleep 语句。
如果您需要 10 秒以外的其他内容,此页面可能对您有所帮助:http://www.crontabgenerator.com/