在 PHP 中,在给定时间后停止执行函数

在 PHP 中,在给定时间后停止执行函数

我的脚本中有五个exec()函数。我想这样设置:如果某个函数在给定时间内没有响应,则该函数将被终止,下一个函数将开始执行。

<?php
    exec("timeout 5 /usr/local/bin/wrun 'uptime;ps -elf|grep httpd|wc -l;free -m;mpstat'",$uptime);  
    exec("timeout 5 /usr/local/bin/trun 'uptime;ps -elf|grep httpd|wc -l;free -m;mpstat'",$uptime);  
    exec("timeout 5 /usr/local/bin/drun 'uptime;ps -elf|grep httpd|wc -l;free -m;mpstat'",$uptime);
 ?> 

在此情况下,该timeout参数不起作用。请更正此问题或提供替代方法。

答案1

你的执行语法错误

string exec ( string $command [, array &$output [, int &$return_var ]] )

你必须在开始你的代码之前设置时间限制,就像这样

<?php

set_time_limit(5);

exec(" /usr/local/bin/wrun 'uptime;ps -elf|grep httpd|wc -l;free -m;mpstat'",$uptime);
?>

相关内容