• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 998
  • Last Modified:

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

0
gr8gonzo
Asked:
gr8gonzo
  • 2
  • 2
1 Solution
 
_Marcel_Commented:
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.
0
 
matt_mcswainCommented:
How about putting

exit

on the last line of your batch file?
0
 
gr8gonzoConsultantAuthor Commented:
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
0
 
_Marcel_Commented:
I think the problem remains that apache/php is waiting for the batch-file to quit, and return a value. I think you don't want apache to be the parent-process of theis batch-file either. So maybe try starting it up with a pipe, directing your output to a file. This should return the value for TRUE back to php, and have the batch-script running on its own. If this doesn't work you might want to try through COM-objects. Something like:

<?
$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run("newBatchFile.bat", 3, false);
?>

If it doesn't like that, you can try making a shortcut (*.lnk file) to the script and run that one...
0
 
gr8gonzoConsultantAuthor Commented:
Nothin has worked, but I'll just close this one out.
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now