|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44942 exec() hangs apache
Submitted: 2008-05-08 05:40 UTC Modified: 2016-07-17 04:22 UTC
Avg. Score:4.5 ± 0.8
Reproduced:73 of 76 (96.1%)
Same Version:27 (37.0%)
Same OS:29 (39.7%)
From: inqualab1985 at gmail dot com Assigned: pajoye (profile)
Status: No Feedback Package: Program Execution
PHP Version: 5.2.5 OS: Windows 2000 SP4
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2008-05-08 05:40 UTC] inqualab1985 at gmail dot com
Hi! PHP Team

I am facing a problem of apache hang up due to exec() function.

I am using PHP 5.2.5 and Apache 2.2 on MS Window 2000 SP4 & also on MS Window XP.

The problem is that I have to execute a exe, for which I have use
exec(). Normally it works Fine, but sometime (randomly) it hang up. It will lock the apache and if we call the same page or any other page of php having exec(), it hang up at the line where exec() function is used. If we commented the line having exec(), then the page run & loaded correctly.

The problem also become critical because there is no error for this.
Also none of the error handling & logging mechanism works.

The only solution to this problem is to restart the Apache.

I am providing you as much as information as possible to catch the problem correctly.

Reproduce code:
$exeFilePath = realpath("../sample.exe");       

if (eregi("win", PHP_OS))
        $command = "\"\"$exeFilePath\" argument1\"";
        $command = "\"$exeFilePath\" argument1";
$output = null;
$returnVal = null;
$result = exec($command,$output,$returnVal);
if($returnVal !=0)
   echo "Error";
   echo $output[0];

echo "end of the program";

Expected result:
it will show the output of exe for particular argument. 

Actual result:
Normally it works correctly as expected but sometime the the execution of the page hang at line "$result = exec($command,$output,$returnVal);"  . After this has happen I am not able to run any script that will use
the exec(). In all scripts apache hangs at exec() function, do not matter  which exe will be called. 


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2008-05-09 14:20 UTC]
What exactly does this "sample.exe" do? It's most likely not any PHP problem at all anyway, you just call some exe that "hangs" itself..
 [2008-05-14 07:56 UTC] inqualab1985 at gmail dot com
Dear Jani

There is no problem with 'sample.exe'. I have tested the same for different exe or even for dos command. It hangs when we are calling the page at regular interval of time (through Ajax).

There is a problem in the definition of exec() function.

For any further information , feel free to contact me.
 [2008-06-10 14:59 UTC] aleaddict at yahoo dot com
Windows Server 2003 R2 Service Pack 1
Apache 2.2.6
PHP 5.2.4 (upgraded to 5.2.6 with no success)

Executing resource kit utility shortcut.exe via exec() with EXACTLY the same results...  Random, no errors logged, hangs up all succeeding calls to exec(), requires restart.

 [2008-07-03 20:33 UTC] markus at opahle dot com
Windows XP SP3
Apache 2.2.6
PHP 5.2.6

I have the same problem. 

I noticed that apache only hangs up when a php script with an exec command was called twice at the same time.

The problem also ocours when I tried to use popen or proc_open(with and without bypass_shell option)
 [2008-07-04 20:28 UTC] inqualab1985 at gmail dot com
Dear markus

In the first sight, I also understand that this happen only when multiple request hit the script with exec(). But in reality there is no guarantee that if the request goes one by one Hanging will not happen. I had done a test at the time of writing this bug in which call the script one by one but it randomly hanged up again after a period of time. At that time period is long.

So dear PHP team , you have to really look up this matter.

 [2008-07-09 13:04 UTC] lk at profline dot ch
We have got the same problem here, exec hangs sometimes and causes the page to not respond to anything. Only way is to restart apache. Is there a solution available for this ?
 [2008-07-09 13:17 UTC] lk at profline dot ch
I forgot the specifications, we use:

Windows XP  
Apache 2.2.8
PHP 5.2.6

Is that an apache or a php related problem ?
 [2008-07-14 21:14 UTC]
Thank you for this bug report. To properly diagnose the problem, we
need a backtrace to see what is happening behind the scenes. To
find out how to generate a backtrace, please read for *NIX and for Win32

Once you have generated a backtrace, please submit it to this bug
report and change the status back to "Open". Thank you for helping
us make PHP better.

 [2008-07-22 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2008-08-08 18:55 UTC] jumpbackhack at gmail dot com
this is still a bug. this is not an apache issue as I have tested various versions ranging from 2.0.x to 2.2.x

I have tested php v5.2.6 and even a late development version.  This happens on linux OS as well even with shell_exec()

would there be a backtrace on a hang?

it appears that the hang happens after a successful script executes, the following time the script runs there is the hang.  usually lasts 2-5 minutes before it allows apache to run any php again.

note that apache will serve static/html files without issue while any php files are not called.

even a restart of apache does not resolve.
 [2008-09-15 17:01 UTC] vlabella at uamail dot albany dot edu
We have been having the same issue for a long time on win2003 with php version 5.2.X and apache 5.2.x calling both perl scripts and .exe with the exec fuinction.  We found that it mostly happens when php runs exec concurrently on the same process.  For example we would have ajax call do an exec on the server to read some data and feed it back to the user every 5 seconds.  In addition the user could do an ajax call to do something as well on this page.  if the user call came at the same time as the schdeduled call then php would hang and we would have to restart apache.  We found it also happens on pages where there are multiple exec() calls.  We find that all the scripts and programs we call run fine its just that php hangs when they finish for some reason.  We have our max_execution_time set to 30 minutes so its not a timeout issue.

We found that calling
before each exec call seemed to resolve this issue.  But we are not really sure why and what other effects session_write_close(); may have on other processes.

Hope this helps resolve this issue.  it is a major headache for us for the past 2 years!!
 [2008-10-21 09:26 UTC] neododge at free dot fr
The same problem happens for me on PHP 5.2.6 / Apache 2.2.9, Apache won't run any PHP after I try an exec/passthru/`` -no matter what command I try. Only way to get PHP back seems to be closing all browser windows that point to my server and waiting for a while before trying to use a PHP page again.
 [2008-12-04 04:35 UTC] dominic dot manley at det dot wa dot edu dot au
Big thanks to vlabella who led us to a work-around. This is one VERY frustrating bug to track down!

We have an Apache/PHP 5.2.3/Win2003 setup and concurrent calls to a script that exec()-ed the same command-line .exe caused the process to get caught up on the server. Apache wouldn't let go of it and the only way to kill the process and get the site/sometimes whole server back was to restart Apache.

I never would have suspected sessions causing this issue... none of our investigations led us close to that direction. However, vlabella got it spot on...

Calling session_write_close() before an exec() (or simply never initiating session support to start with using session_start()) works around the issue.
 [2009-01-15 16:54 UTC] bill at sammer dot com
session_write_close fixes it for me too.  Thanks vlabella!
 [2009-01-29 18:21 UTC] rolinger1 at hotmail dot com
I came across this article searching for a similar problem. I am using Perl 5.10 though but am having the same issue with Apache 2.2.  That being said, I believe the issues are the same and that the problem is with Apache and not php or perl.

With Perl I do a backtick call and Apache's CGI hangs.  The windows system processes shows 1 cmd running and 1 perl app running for each time I click on the called web script.  

Perl call:
@files = `dir ../configs /0:D /A:-D` ;

Somehow, apache uses the backtick command to launch a "cmd" console process which in turn executes the system call "dir..."

I say the Apache CGI hangs because other PHP scripts (that don't do system calls) all still seem to work.  But after the first CGI hang, all other Perl calls hang - even scripts that don't have system calls in them.  Scripts with system calls load another pair of cmd/perl processes and return no data, scripts without system calls loads another perl process but does return data.  

After the initial hang though, all related processes (cmd or perl) remain in memory and eventually Apache just hangs altogether.  And when that happens we are forced to do a system reboot - simply doing a service restart has won't work.
 [2009-02-17 10:12 UTC] russell_anam at hotmail dot com
I can indeed confirm that this bug exists in the latest PHP version (PHP 5.2.8). exec() would randomly hang the Apache and the only solution was to restart the whole server (service restart did not help). I can also confirm that vlabella's solution INDEED worked for us. Since this workaround works I think it's safe to say this is indeed a PHP bug that needs to be fixed ASAP.
 [2009-02-26 15:14 UTC] frase at cs dot wisc dot edu
This is happening to me as well, PHP 5.2.8, Apache 2.2.11, Windows 2000 Pro SP4.

However, it only seems to happen when PHP is configured as an Apache module (php5apache2_2.dll); when I configure it as CGI, it works fine.

I also have anecdotal evidence that it's related somehow to other sockets being open on the system.  In my case, we have a VB (compiled) application with an embedded IE control, and also opens the serial port to communicate with some hardware.  If that hardware is plugged in (and PHP is loaded as a module), then our scripts hang at 'exec("ping -n 1 somehost")'.  If I unplug the hardware (so the serial port can't be opened), it works.
 [2009-04-07 15:12 UTC] gouldee at netscape dot com
I am having the same or similar issue:
    OS: MS2003
    PHP: 5.2.6
    APACHE2: 2.2

The bug occurs with an PHP script that has a PHP passthru exec call that hangs every now and then.  After the first hang occurs then any passthru call hangs in the same manner.

I believe that the passthru call runs as I am calling a sendmail.exe process that does send the E-Mail, however, the Browser page hangs after the E-Mail has been sent by sendmail.exe called via passthru().

There are no PHP or Apache2 errors or warnings.  It seems that the problem happens when (2) calls to the same .exe happens at the same time, but, I have no real proof of this, as I can't provide execution times for each instance of the sendmail.exe running.

This is a serious problem, as all pages that call an .exe process after the initial hang will hang up as well. I can clear the bug by simply stopping and restarting the Apache service.

I also note that If I run the page, that hangs up, manually in a Command Prompt DOS window like:  D:\php page.php there are no hangs and the process performs correctly even during and after the first showing of the bug.  So the problem seems to be when PHP does an exec call in conjunction with Apache, as I said even during the time a hang occurs I can still exec passthru commands from a command prompt.

I can also run any page that calls PHP as long as the page does not have any exec or passthru calls in it, and all pages with HTML only runs fine as well during the hang period.
 [2009-06-08 16:41 UTC] alex at bartl dot net
Reproducable with PHP 5.2.9-1 on Windows2003 Server with Apache2.2
Workaround with calling session_write_close() before calling exec()
confirmed working

NOT reproducable with PHP 5.2.1 on Windows 2000 Server with IIS5

anyway, seems to be a duplicate of Bug#44994
 [2009-08-27 10:00 UTC] listephp at andrioli dot com
I've the same problem too.
I tried the latest snapshot 5.2.11RC2-dev build date: Aug 26 2009 23:42:35 , but it shows up.

- Windows 2003 R2
- Apache 2.2.11

Both, exec() running java.exe and system() running .NET program, hang Apache as described above.
 [2010-07-25 21:00 UTC] andy dot hilbig at clubsys dot co dot uk
I have been plagued with PHP freezing after a few minutes, days or maybe weeks. 
I use exec() to link to custom vb.exe
I have searched and searched until today tried the session_write_close(); before the exec()call and session_start (); after.

I can now click [Next Day]/[Next Day] as fast as I like and all appears solid now. Other users have connected at the same time.

So, thank you all very much for the cure.

I have umpteen years programming in machine code and 15 in VB6 with the Jet engine to Access. All works well with Apache/PHP/VB exes. Do I really need .Net ???
 [2010-09-06 14:53 UTC] elwiz at 3e dot pl

I am also having the same problem. One of my scripts is being called multiple times (as a SRC for image tags) and each time it uses the exec function. On windows XP, with PHP 5.2.5 and Apache 2.2 the PHP stops responding. Requests for static files can be done but PHP scripts run endlessly. Session closing and opening helps but it is still just a workaround.
 [2010-09-07 00:13 UTC] richard at dlconsulting dot com
Had a similar problem which was reproducible on Apache 1.3.26 and PHP 5.2.14 or PHP 5.3.3, (Apache running on Windows XP SP3). Reproducable when using Firefox 3.6.8, Chrome 5.0.375.127, IE 8.0.6001.18702 Interestingly, when the same PHP based system was running on linux (Centos 5.5) this problem could not be reproduced.

A parent browser window opens a child popup to perform an import process of some files into a system. The child popup window is running a PHP script which as part of its process calls an external binaries such as rm.exe or unzip.exe binary using the exec function.

As part of the html in the child popup window, we call a javascript function with <script type="text/javascript">window.opener.location.reload();</script>
which reloads the parent window (which is also generated via PHP).

When this bug occurs, the JavaScript above is near the top of the web page, so that it seems to trigger a reload of the parent PHP script during the child popup PHP process, and apparently this interferes somehow with the execution of the child popup PHP process, because when we tried moving this JavaScript to the bottom of the popup PHP process webpage . . the problem goes away.

This problem, which seems somehow related to the concurrent execution of the PHP of the parent browser window, hangs Apache completely. Apache has to be terminated manually, and then restarted. Therefore this bug was a showstopper until we happened upon a work-around.

If we remove calls to exec() in the child popup PHP script, the problem disappears. The duration of the child popup PHP script is not trivial, it might unzip / read / move maybe 50 files (although on fast hardware this might take less than a second anyway . . .)
 [2011-01-18 09:50 UTC]
-Status: No Feedback +Status: Duplicate
 [2011-01-18 09:50 UTC]
See #44994
 [2011-10-10 15:30 UTC] jameslively at insightbb dot com
I am still seeing this issue on PHP 5.3.8.  I am running the site on Windows 2k3 sp3 and Apache 2.2.
 [2011-10-23 19:26 UTC] kevin at texasbb dot com
Happening to me on windows server 08 using IIS

session_write_close(); doesnt seem to work for me.
but i can use a page with iframes and eatch iframe can have a php with exec()
and all work.
i tested 10 different php iframes and could see all 10 processes open when page 
was loading.
when adding just 2 exec() calls on same script or even trying to use different 
php files called from 1 will freeze the second process from exec

just throwin this out there to help solve this

so iframes can call any number it seems but
more than 1 in same php script will hang on SECOND process(no need to reset 
server just kill the process and output from first will be there but second will 
not and page will finish loading).
 [2012-06-04 05:09 UTC] hshs at hotmail dot com
This bug still exist. Same work around still works.
 [2012-09-24 08:28 UTC] mail at GerhardBechtold dot com
Despite various updates and fixes, I dont think, the 'exec problem' in PHP is solved.

In my PHP programs, I have plenty of calls from PHP 5.3.0 to external exe files with 'exec(...)' (on local drive, with Apache 2.2.12, installed through xampp 1.7.x, on Vista and Win 7) on dozens of computers: Everything works well.

But when I migrated to new computer with PHP 5.4.4 (VC9, with Apache 2.4.2, installed through xampp 1.8, on Win 7/64), the SAME exec() calls dont work anymore. No display, no error messages, no entries in log file. Configuration in httpd.conf and php.ini are identical.

Any advice is appreciated. I also posted in xampp forum (, with confirmation, that I am not the only one with this problem....).

Kind regards
 [2012-09-27 06:57 UTC]
We need something to reproduce it, as we can't.

Do you have a common exe app that we can use to try to reproduce the problem you 
are experiencing?
 [2012-09-27 10:39 UTC] mail at Gerhard-Bechtold dot com
Thanks for follow-up. Where can I send it to?
 [2012-09-27 10:51 UTC]
Tell us which command you use and how you call it, along with the parameters 
(external files and the likes can be sent to my email (my handle @
 [2012-09-27 21:02 UTC] mail at GerhardBechtold dot com
Thanks for looking into my problems with exec() at the latest PHP.

Example for actual code, as in application (was running nicely in earlier PHP installations, but not under PHP 2.4 anymore):

$str1Name = "C:\Map Maker\MMmacro.exe";	
$str2Name = "command=remove layer";		
I am using a GIS called Map Maker, with powerful MMmacro functions ( 'remove layer' is one of the most basic parameters of MMmacro.

A more simple test version also failed:

$str1Name = "C:\Windows\Notepad.exe";

I also tested, all without success:
- with bat file, which then calls notepad.exe
- with exe/bat in different folders: document root (C:\xampp\htdocs) or from calling directory (C:\xampp\htdocs\lupmis_s)
- with 'start .....'
- with popen
- with exec( .... < > file.out 2> nul", $output);
- with exec(....,$output, $return);
- with exec("ping", $output, $return);


My environment:

PHP 5.4.4 (VC 9)
Apache 2.4.2
Xampp 'default' installation 1.8 on C:\xampp
Apache with PHP, MySQL running as services

Win 7 Home Premium / 64 bit
HP Pavilion g7 Notebook
Bought in Germany, but OS and all programs installed in English
Running as administrator

Total application has > 220 PHP programs, with > 250 exec calls. 

If you give me your full email address, I can also send you the php.ini and httpd.conf.

Thanks for any advice
 [2012-10-14 15:28 UTC] mail at GerhardBechtold dot com
After many hours of testing, I managed to solve my problem of exec() calls in PHP. This might be useful also for other developers, as I have seen many struggling with te same problem.

1. xampp must be installed in real (!) administrator mode (in Windows 7 / 32 & 64 bit). 

2. In many environments, Apache and MySQL should not run as services, but be manually started (even not with interaction with desktop).

I have put a step-by-step procedure to troubleshoot at:

(Ignore the references to our system LUPMIS).

Good luck to everybody 
 [2012-10-15 09:12 UTC]
@mail at GerhardBechtold dot com

this is documented, the shell/exec permissions have to be given.

However I do not think it is related to the original issue which is caused by a 
real bug in the php stream, where the eof of the console stream is not correctly 
detected and ends in an endless loop.
 [2012-10-15 15:00 UTC] mail at GerhardBechtold dot com

I didn't find any documentation on the service sensitivity of the new PHP.

You might be right, that the eof of the console stream is not taken care properly, but in my case the system is now working (again).
 [2012-12-05 12:04 UTC] claudix dot kernel at gmail dot com
Seen the same behavior, not only in exec(), but also in similar functions as proc_open/proc_close. When there are concurrent scripts during a same PHP session, the script spawning the process randomly hangs.
    - Windows 2003 server SP2
    - Apache 2.2.22/ PHP 5.4.3

I can confirm that calling session_write_close() and then session_start() does the trick. 

I've observed, though, that the code below doesn't work:

    $proc = proc_open($cmd,$pipedesc,$pipes);    
    //do stuff with pipes... 
    //... and close pipes
    session_write_close();  //Close session before hanging function
    $retval = proc_close($proc);
    session_start(); //restore session

But the code below *does* work:
    session_write_close();  //Close the session before proc_open()
    $proc = proc_open($cmd,$pipedesc,$pipes);
    //do stuff with pipes... 
    //... and close pipes
    $retval = proc_close($proc);
    session_start(); //restore session

This made me go into the PHP source code (actually the source file "proc_open.c"). I've noticed that the command passed to proc_open() is spawned by calling the WINAPI function CreateProcess(...) with the parameter "bInheritHandles" set to TRUE. As of MSDN documentation, if this parameter is TRUE then all handles are inherited by the child process. It seems that the handle of the session is being inherited by the child process but for some reason the OS doesn't release it when the process ends, eventually yielding a deadlock. The code snippets above show this: the session has to be closed before calling proc_open() to prevent the spawned command from inheriting the session handle. People using exec() cannot see this effect because exec() virtually embeds proc_open/proc_close.

May this give a clue to PHP developers?

 [2012-12-06 12:54 UTC]
See also #44994
 [2012-12-06 12:56 UTC]
-Status: Duplicate +Status: Assigned -Assigned To: +Assigned To: pajoye
 [2015-03-23 21:18 UTC] yardsticks at gmail dot com
Same issue here, years later... I built a whole script around the expectation that this would work. The solution posted did not work for me either :(
 [2015-03-24 02:39 UTC]
This should be fixed already. Please try latest 5.6
 [2016-07-03 05:53 UTC]
-Status: Assigned +Status: Feedback
 [2016-07-03 05:53 UTC]
this should be fixed in latest.
 [2016-07-17 04:22 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 [2022-03-11 13:33 UTC] aploo at pagentra dot com
i'm using linux server and whenever i call exec() it hang my apache server and i have to restart my apache server manually so please help
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Mar 02 21:01:29 2024 UTC