5

I've stumbled upon a phenomenon that I can't explain myself.

I'm using popen to execute php and then execute a php script that way, and pclose to close that. So far so fine. I ran into quite some severe troubles as the script where I used this didn't execute and instead after trying it 3 times in a row I crashed the zend-server (no page would open any more). I found out that the reason to this was that I used a wrong directory for the php.exe. Example:

if (pclose(popen("C:\wrongDir\php\php.exe C:\Zend\Apache2\htdocs\myApp\public\mytest.php 57 > C:\Logs\1\0\jobOut.log 2> C:\Logs\1\0\jobErr.log"))>-1)
{
.....
}

Aside from the "wrongDir" all other dirs were correct....the popen even created the jobOut and jobErr files (which were empty). (remark: PHP is not in a searchpath that is why it wasn't found without the correct path)

Even though I now solved the problem....I have the question if this is a normal behaviour there, or if I have done something wrong (maybe even server settings). As from what I read in the manual about both commands it sounded to me that in the my case I should have had a return value of either -1 or 0 and not the problem I ran into with the process and then the server hanging).

Thanks.

Thomas
  • 2,886
  • 3
  • 34
  • 78

1 Answers1

1

It appears that pclose() doesn't return the exit status of the process but rather of it's ability to close the process.

To get the process termination 'code' use pcntl_wifexited() and pcntl_wexitstatus()

http://php.net/manual/en/function.pclose.php
http://php.net/manual/en/function.pcntl-wexitstatus.php

ethrbunny
  • 10,379
  • 9
  • 69
  • 131
  • Interesting although one question would remain there. Why does it crash there (thus the child process hangs. Although if I type in the same command into the commandline it ends with an error and I come onto the commandline prompt again....no hangup there. So that part of my question would be if that is a normal behaviour there or if it is a possible configuration error as when I run the errornous command via php the child process stays active indefinitely) Thus as the process is still "running" it should not have generated an exit code there. – Thomas Dec 11 '12 at 11:30
  • I wonder if nesting the popen/pclose is causing problems. Does it still crash if you split this into several lines? – ethrbunny Dec 11 '12 at 14:06
  • So couldn't test it out on zend server (that one isn't available currently) but I was able to test a bit on xampp. The problem is not caused directly by popen/pclose! It is more caused by the webserver itself as it seems. When the nonexisting exe is called a error message alertbox opens on the server (made by the webserver itself....thus on the zend server I didnt see it but on my local machine with a local webserver now). And that held the process open so that it didn't close. For the pcntl_wifexited() am I correct there that I would need to redo so that I use pcntl_fork for multiple procs? – Thomas Dec 13 '12 at 09:09
  • Seems reasonable. The popup window thing is v odd. – ethrbunny Dec 13 '12 at 12:27
  • It surprised me as it should normally end up in the apache error logs and not in a popup (at least that is what I would have expected there) – Thomas Dec 16 '12 at 13:41
  • So this was windows? Maybe it's not running as a service? – ethrbunny Dec 16 '12 at 15:41
  • The xampp I used on my local machine is not running as a service, but the zend-server on the server is running as a service. – Thomas Dec 17 '12 at 11:29
  • That might be way you saw a popup instead of a log message. – ethrbunny Dec 17 '12 at 14:17
  • Possible although the phenomenon was the same on the zend-server (although I can't say if also a popup was there or not only the phenomeonon about not ending was the same there that I know of). – Thomas Dec 18 '12 at 08:37