Link to home
Start Free TrialLog in
Avatar of gr8gonzo
gr8gonzoFlag for United States of America

asked on

Exec randomly hangs / stalls

My environment is:
- PHP 4.3.10
- Apache 2.0.53
- Windows 2000 Server
- The Apache service is configured to run as "Local System account" with "Allow service to Interact with desktop" turned ON

I have a script that runs several commands via the exec() function. The script basically does something like this:

----------------------------
exec("MOVE someFiles tempDir");

exec("DIR tempDir /some switches",$CaptureDirOutput);

$fp = fopen("newBatchFile.bat","w");
fwrite($fp,"Some \r\n");
fwrite($fp,"Complex \r\n");
fwrite($fp,"Commands \r\n");
fwrite($fp,"exit \r\n");
fclose($fp);

exec("newBatchFile.bat");

exec("MOVE resultingFiles finalDir");
----------------------------

So there are 4 exec() calls in there. For about a week, this worked perfectly. When we ran the script, we could see the shells being opened and closed on the desktop. The script runs on a server that is nothing more than a little-used database server, so there have been no changes made to the server, really.

Suddenly, these same exec() calls started to hang. We can see the first shell window open (to move files), and it DOES execute the program (the files get moved), but then the shell just stays open and doesn't close down.

Again, nothing has changed that would affect the call. Sometimes the filename might be 123321.xml, and sometimes it will be 1439894.xml, or something like that. And the fact that the command actually runs (for instance, an exec("MOVE files newdir") call WILL actually move the files), means that it's something else.

The only other odd thing I've noticed is that I have 2 Apache.exe processes in my Task Manager - the first is taking up about 25,000k of memory, the second is just under 7,000k. But after restarting Apache, it still seems to create 2 processes (they just don't use as much memory yet).

Any ideas?

- Jonathan

Avatar of _Marcel_
_Marcel_
Flag of Netherlands image

First of all a small comment: PHP is a wonderful language for creating web-applications, and it can be used in other situations. I would not recommend using PHP for scripts like these, although it might work al right. But this is just a minor note.

Your question regarding the apache processes: this is how apache works. Remember it is not a fully threaded webserver. For every process that has to run, it creates a child-process. Apache will make sure enough child-processes are ready for accepting calls. You could actually influence this behaviour in the httpd.conf. But this all is normal behaviour. If you see a process that consumes more memory/cpu it is because a script is running in that process and that script is consuming that.

Another thing is that I find it odd to use exec calls to move files. To move a file using php you can simply use the rename() function. For reading directories or getting information about files php also has functions like scandir() and stat(). In this way you can circumvent executing command and not easily getting the return codes back to PHP. I have experienced problems with windows concerning this before. If you incorporate your last move command in your batch file (wich shouldn't be that hard), you could even run the created batch-file in background mode. You won't need to be concerned about receiving the return code then in order to perform the complete steps.
Avatar of matt_mcswain
matt_mcswain

How about putting

exit

on the last line of your batch file?
Avatar of gr8gonzo

ASKER

Hi Marcel,

The main application IS a web application (database-driven). There is just one part of the application that uses this set of exec() calls. I know it is odd to use exec() for some simple functions, which is why I already rewrote the routine to use internal PHP functions whenever possible, but I did not want to add that and make my original question more confusing.

The batch file is the only exec() call left, but even it hangs sometimes. It seems to run okay for a bit after I restart Apache, but eventually it will get to the point of hanging again. Unfortunately, there is no way around this last exec() call.


matt_mcswain - I already have exit on the last line of my batch file, but it sometimes hangs on the 2nd or 3rd line of the batch file, so it never even gets to the exit command.


I have a strong feeling this problem is tied to resources that are not being freed up, but I'm not sure where to start looking to track down Apache resource leaks.

- Jonathan
ASKER CERTIFIED SOLUTION
Avatar of _Marcel_
_Marcel_
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Nothin has worked, but I'll just close this one out.