jo phul jo phul - 1 year ago 93
PHP Question

PHP line pclose(popen(... won't work on Amazon AWS with MAMP

I have MAMP installed and am running a php script to execute external programs (jar and exe). Everything works perfectly on my local machine.
However when I put it all on the AWS instance, it just ignores the pclose(popen(...)) line. I get both log messages and no errors.

$command = 'start /B cmd /C '.$this->getCommand().' >NUL 2>NUL';
$this->Log.debug('executing command : '.$command);
pclose(popen($command, 'r'));
$this->Log.debug('command sent');

I'm thinking it might be a permissions issue of some sort? But I've checked the php.ini and the Apache permissions and everything looks good. Php is not being told to ignore the popen command.

I created a test.bat file in the same location

echo Hello, World > donald.txt

and this executes when called from php, so now I know the pclose(popen()) is working. The command I am trying to execute is java - jar ... so it must be something to do with permissions running java?

So this works:

start /B cmd /C D:/programs/test.bat >NUL 2>NUL

and this does not:

start /B cmd /C java -Xmx2048m -jar "D:\programs\program.jar" -pm XXX_YYY -t FULL -flag1 -flag2 -k 23 >NUL 2>NUL

Answer Source

Remove everything that is unnecessary, to make your command look like this:

$command = 'start /B ' . $this->getCommand();

you don't need any redirection as start /b will start program without new window.

I have the following:

$cmd = 'start /b php {$file} {$bid}';
pclose(popen($cmd, 'r'));

And it works on windows perfectly.

BTW, for Linux-based machine you need to run command like this:

$command = $this->getCommand() . ' &';

to make one script works on windows and Linux you need something like this:

$command = $this->getCommand() . ' &';
if (false !== stripos(PHP_OS, 'win')) {
    $command = 'start /B ' . $this->getCommand();
$this->Log.debug('executing command : '.$command);
$p = popen($command, 'r');
sleep(10); //we will give some time for process to start
$this->Log.debug('command sent');

UPDATE: in the chat we've figured out that the problem was with path to java.exe. Apache was unable to find java executable. so the solution was to specify absolute path to java.exe:

$command = 'start /B C:/Progra~1/Java/jre1.8.0_102/bin/java.exe -Xmx2048m -jar "C:/programs/some-jar-file.jar" -?';
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download