1

I have JDK and I am trying to execute Main.java program which contains infinite loop and I want to break java Main.java < input.txt > output.txt command if it goes into infinite loop and if not infinite loop than dont won't to break program.. Any solution ?? In trouble

<?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
}

?>
1
  • You want a solution? Don't let it go into infinite. Other solution would be to kill the process if you know the process ID. Commented Apr 19, 2016 at 17:41

3 Answers 3

1

Try wrapping it in a class like this (essentially wrapping it in nohup COMMAND > /dev/null 2>&1 & echo $! to get the pid and work with it that way in the background)

<?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;
    }
}
?>
Sign up to request clarification or add additional context in comments.

3 Comments

And how to run my program using Process?
No this won't work on Windows. To do something similar on Windows to put in the background you can try something like pclose(popen("start /B ". $cmd, "r"))
And to use it you would just run your command with new process and set a loop while ($process.status()) { // kill if timed out } or similar. See the examples at the top of the snippet
1

This is only a suggestion. There will be better answer for your question.

Inside the java infinity loop check some value from other text file. Its like

while true {

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

If you can set your_txt_file.txt value to false of what ever except true then java loop will be break.

Comments

0

You need to open a port to listen to in java, and then connect and send something to that port from php.

Check out @TomaszNurkiewicz answer here where he says

"""

What you probably want is to create a ServerSocket and listen on it:

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

The second line will block until some other piece of software connects to your machine on port 4000. Then you can read from the returned socket. Look at this tutorial, this is actually a very broad topic (threading, protocols...)

"""

and for opening up the socket with php you can use the code (or somethign close to it) provided here by @sanmi from 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);
?>

"""

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.