java - 使用超时处理程序从 PHP 调用 Java

标签 java php unix timeout

问题:我正在通过 PHP 运行用户提交的 java 文件。 java 文件有可能导致无限循环。我该如何在php执行过程中处理这个问题?

这是我的代码:

$proc = proc_open($javaCmd, array(array("pipe", "r"), array("pipe", "w"), array("pipe", "w")), $pipes);
// Check status here logic ?
proc_close($proc);

它当前正在等待该过程完成,但我希望它在 30 秒或一分钟后超时。我试过set_time_limit(x) ,但这不会终止 java.exe

我的问题是,我是否可以在X秒后检查java.exe进程的状态,如果它仍在运行则终止?或者,我需要使用 timeout functionality在java中(即有一个主类在线程上执行用户提交的类)

最佳答案

是的,这是可能的。我不认为 java 进程在这方面与任何其他进程有什么不同。请参阅这些链接 unix exec with timeoutwindows exec with timeout .

我没有编写这段代码,但这里是复制粘贴的,以防原件从互联网上消失:

对于 UNIX:

<?php
  function PsExecute($command, $timeout = 60, $sleep = 2) {
        // First, execute the process, get the process ID

        $pid = PsExec($command);

        if( $pid === false )
            return false;

        $cur = 0;
        // Second, loop for $timeout seconds checking if process is running
        while( $cur < $timeout ) {
            sleep($sleep);
            $cur += $sleep;
            // If process is no longer running, return true;

           echo "\n ---- $cur ------ \n";

            if( !PsExists($pid) )
                return true; // Process must have exited, success!
        }

        // If process is still running after timeout, kill the process and return false
        PsKill($pid);
        return false;
    }

    function PsExec($commandJob) {

        $command = $commandJob.' > /dev/null 2>&1 & echo $!';
        exec($command ,$op);
        $pid = (int)$op[0];

        if($pid!="") return $pid;

        return false;
    }

    function PsExists($pid) {

        exec("ps ax | grep $pid 2>&1", $output);

        while( list(,$row) = each($output) ) {

                $row_array = explode(" ", $row);
                $check_pid = $row_array[0];

                if($pid == $check_pid) {
                        return true;
                }

        }

        return false;
    }

    function PsKill($pid) {
        exec("kill -9 $pid", $output);
    }

对于 Windows:

<?php
// pstools.inc.php

    function PsExecute($command, $timeout = 60, $sleep = 2) {
        // First, execute the process, get the process ID
        $pid = PsExec($command);

        if( $pid === false )
            return false;

        $cur = 0;
        // Second, loop for $timeout seconds checking if process is running
        while( $cur < $timeout ) {
            sleep($sleep);
            $cur += $sleep;
            // If process is no longer running, return true;
            if( !PsExists($pid) )
                return true; // Process must have exited, success!
        }

        // If process is still running after timeout, kill the process and return false
        PsKill($pid);
        return false;
    }

    function PsExec($command) {
        exec( dirname(__FILE__). "\\psexec.exe -s -d $command  2>&1", $output);

        while( list(,$row) = each($output) ) {
            $found = stripos($row, 'with process ID ');
            if( $found )
                return substr($row, $found, strlen($row)-$found-strlen('with process ID ')-1); // chop off last character '.' from line
        }

        return false;
    }

    function PsExists($pid) {
        exec( dirname(__FILE__). "\\pslist.exe $pid 2>&1", $output);

        while( list(,$row) = each($output) ) {
            $found = stristr($row, "process $pid was not found");
            if( $found !== false )
                return false;
        }

        return true;
    }

    function PsKill($pid) {
        exec( dirname(__FILE__). "\\pskill.exe $pid", $output);
    }

关于java - 使用超时处理程序从 PHP 调用 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12028993/

相关文章:

java - 哪个版本更好?

PHP:如何以给定格式转换这句话

php - 需要知道要在 codeigniter 中执行哪个查询

unix - 如何从任何位置执行 bash 脚本?

linux - 如何在 tail -f 中只显示新记录的内容?

java - 从 Java 实例化一个 Scala 类,并使用构造函数的默认参数

java - 在 JSON 字符串中插入节点

linux - 为什么 Linux grep 没有给出正确的换行符计数?

Java - 只读取文件的第一行

php - 我的网站 (PHP) 上的赌博应用程序出现问题