1

am executing a shell command like below.

shell_exec('java -jar sanityTest.jar');
$success = array('status' => "Success",'type' => "execute");
echo json_encode($success);

The shell_exec command not going to next statement until the execution complete. What I want is to execute it in background, even for Windows.

I tried

shell_exec('java -jar sanityTest.jar >/dev/null 2>/dev/null &');

which is coming to next line but not executing the command.

J. Chomel
  • 8,193
  • 15
  • 41
  • 69
  • 1
    http://stackoverflow.com/questions/3819398/php-exec-command-or-similar-to-not-wait-for-result – Janno Aug 03 '16 at 06:27
  • function execInBackground($cmd) { if (substr(php_uname(), 0, 7) == "Windows"){ pclose(popen("start /B ". $cmd, "r")); } else { exec($cmd . " > /dev/null &"); } } – user2786092 Aug 03 '16 at 06:56

1 Answers1

1

My solution: use start /B *my command*

function execInBackground($cmd) { 
  if (substr(php_uname(), 0, 7) == "Windows"){ 
    pclose(popen("start /B ". $cmd, "r")); 
  } else { 
    exec($cmd . " > /dev/null &"); 
  } 
}

I could have found it by looking at the right keywords: https://stackoverflow.com/a/21031259/6019417

Community
  • 1
  • 1
J. Chomel
  • 8,193
  • 15
  • 41
  • 69