Exec randomly hangs / stalls

Posted on 2005-03-30
Medium Priority
Last Modified: 2013-12-13
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");


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

Question by:gr8gonzo
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2

Expert Comment

ID: 13679054
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.
LVL 11

Expert Comment

ID: 13686901
How about putting


on the last line of your batch file?
LVL 35

Author Comment

ID: 13689579
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

Accepted Solution

_Marcel_ earned 750 total points
ID: 13692771
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...
LVL 35

Author Comment

ID: 14351969
Nothin has worked, but I'll just close this one out.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question