【问题标题】:Break infinite loop java program using PHP exec command使用PHP exec命令打破无限循环java程序
【发布时间】:2016-04-20 01:52:26
【问题描述】:

我有 JDK,我正在尝试执行包含无限循环的 Main.java 程序,如果它进入无限循环,我想打破 java Main.java output.txt 命令,如果不是无限循环比不会破坏程序..任何解决方案?遇到麻烦

<?php

exec('cmd /k c:/wamp/www/javac Main.java 2>&1', $outputAndErrors, $return_value);
for($i=0 ; $i<sizeof($outputAndErrors) ; $i++)
{
    $output1=htmlspecialchars($outputAndErrors[$i],ENT_QUOTES);
    echo "$output1";    
    $flag=1;
}
if(!$flag)
{
    exec('cmd /k c:/wamp/www/java Main.java < input.txt > output.txt', $outputAndErrors, $return_value);
    //want to give timeout but if exec goes to infinite loop than below statement will not executed
}

?>

【问题讨论】:

  • 您需要解决方案吗?不要让它进入无限。如果您知道进程 ID,其他解决方案是终止进程。

标签: java php


【解决方案1】:

尝试将其包装在这样的类中(基本上将其包装在 nohup COMMAND &gt; /dev/null 2&gt;&amp;1 &amp; echo $! 中以获取 pid 并在后台以这种方式使用它)

<?php
    // You may use status(), start(), and stop(). notice that start() method gets called automatically one time.
    $process = new Process('ls -al');

    // or if you got the pid, however here only the status() metod will work.
    $process = new Process();
    $process.setPid(my_pid);
?>

<?php
    // Then you can start/stop/ check status of the job.
    $process.stop();
    $process.start();
    if ($process.status()){
        echo "The process is currently running";
    }else{
        echo "The process is not running.";
    }
?>

<?php
/* An easy way to keep in track of external processes.
* Ever wanted to execute a process in php, but you still wanted to have somewhat controll of the process ? Well.. This is a way of doing it.
* @compability: Linux only. (Windows does not work).
* @author: Peec
*/
class Process{
    private $pid;
    private $command;

    public function __construct($cl=false){
        if ($cl != false){
            $this->command = $cl;
            $this->runCom();
        }
    }
    private function runCom(){
        $command = 'nohup '.$this->command.' > /dev/null 2>&1 & echo $!';
        exec($command ,$op);
        $this->pid = (int)$op[0];
    }

    public function setPid($pid){
        $this->pid = $pid;
    }

    public function getPid(){
        return $this->pid;
    }

    public function status(){
        $command = 'ps -p '.$this->pid;
        exec($command,$op);
        if (!isset($op[1]))return false;
        else return true;
    }

    public function start(){
        if ($this->command != '')$this->runCom();
        else return true;
    }

    public function stop(){
        $command = 'kill '.$this->pid;
        exec($command);
        if ($this->status() == false)return true;
        else return false;
    }
}
?>

【讨论】:

  • 如何使用 Process 运行我的程序?
  • 不,这在 Windows 上不起作用。要在 Windows 上执行类似的操作以放入后台,您可以尝试类似 pclose(popen("start /B ". $cmd, "r"))
  • 要使用它,您只需使用新进程运行命令并设置循环while ($process.status()) { // kill if timed out } 或类似的。请参阅 sn-p 顶部的示例
【解决方案2】:

这只是一个建议。您的问题会有更好的答案。

在 java 无限循环中检查来自其他文本文件的一些值。就像

while true {

v = readFile('your_txt_file.txt')
if v == "true" {
    break;
}
//do your stuff   }

如果您可以将 your_txt_file.txt 的值设置为 false,除了 true 之外的任何值,那么 java 循环将被中断。

【讨论】:

    【解决方案3】:

    你需要在java中打开一个端口来监听,然后从php连接并发送一些东西到那个端口。

    查看@TomaszNurkiewicz 回答here 他所说的地方

    """

    您可能想要的是创建一个 ServerSocket 并监听它:

    ServerSocket serverSocket = new ServerSocket(4000);
    Socket socket = serverSocket.accept();
    

    第二行将阻塞,直到其他一些软件在端口 4000 上连接到您的计算机。然后您可以从返回的套接字中读取。看看这个教程,这其实是一个很宽泛的话题(线程、协议...)

    """

    要使用 php 打开套接字,您可以使用 @sanmi 提供的代码(或接近它的代码)here 来自 the manual

    """

    <?php
    error_reporting(E_ALL);
    
    /* Get the port for the WWW service. */
    $service_port = getservbyname('www', 'tcp');
    
    /* Get the IP address for the target host. */
    $address = gethostbyname('www.example.com');
    
    /* Create a TCP/IP socket. */
    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    if ($socket === false) {
        echo "socket_create() failed: reason: " . 
             socket_strerror(socket_last_error()) . "\n";
    }
    
    echo "Attempting to connect to '$address' on port '$service_port'...";
    $result = socket_connect($socket, $address, $service_port);
    if ($result === false) {
        echo "socket_connect() failed.\nReason: ($result) " . 
              socket_strerror(socket_last_error($socket)) . "\n";
    }
    
    $in = "HEAD / HTTP/1.1\r\n";
    $in .= "Host: www.example.com\r\n";
    $in .= "Connection: Close\r\n\r\n";
    $out = '';
    
    echo "Sending HTTP HEAD request...";
    socket_write($socket, $in, strlen($in));
    echo "OK.\n";
    
    echo "Reading response:\n\n";
    while ($out = socket_read($socket, 2048)) {
        echo $out;
    }
    
    socket_close($socket);
    ?>
    

    """

    【讨论】:

      猜你喜欢
      • 2013-03-08
      • 1970-01-01
      • 2012-04-02
      • 1970-01-01
      • 2016-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-01
      相关资源
      最近更新 更多