exec php

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I have a php site. Some page needs to call an external program. The
programs are home made c# applications.

It uses to  work without problem. For a while. Maybe it work for some
hour. Or for a day. Or even for a week.

At a certain point, when a php script calls the exe file, the
application freezes. The following happens:

   $return_value = exec('path_to_exe_file');
   echo $return_value;

Well, the exec is executed (I can see that, the exe fil does what it
has to do). But it doesnt return to the php-script. The php-script
waits infinitely that the app will return, but it will not return. At
a certain point the time execution exeedes.

If I go to the command line and execute an exe file (the same one that
exec calls), then it works without problem, and returns immediatly.
But from the exec call it doesnt return.

How do I solve this? Well, I restart the server. And hen everything
works again. Which means that the script can use exec again without
waiting infinitely.

When the problems begin (when the exec begins to fail) the server can
still work without problem. All the scripts run. The only scripts that
dont run are those that have the exec call inside.

error.log doesnt says anything about this.

Im running apache 2.X on windows server 2003. PHP version is 5.x.

I have put log function before and after every single call to exec. I
analyzed it, but I can not understand why the exec fails. It only
stops to get back the return value from the c# app. The c# app can
also be a simple one with only a console.write (something like echo).
But when exec has hanged up neither that will work.

Someone can see the problem?

Re: exec php

On Dec 21, 3:01 pm, wart...@hotmail.com wrote:
Quoted text here. Click to load it

Bad combination! Apache server on Windows 2003 server. Thy combination
really diminishes the performance and may cause those freezes

You should switch to GNU/Linux-Apache/php or Windows2003/IIS/Asp.NET

Re: exec php

Betikci Boris wrote:
Quoted text here. Click to load it

Horse hockey. Apache runs quite well on Windows - much better than IIS does.

But I do agree Linux is a better os.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Re: exec php

On Fri, 21 Dec 2007 05:01:05 -0800 (PST), warth33@hotmail.com wrote...
Quoted text here. Click to load it

Since the exec() function isn't expecting anything back, it might be worth
trying either "shell_exec()" or "system()" since those would get back some
output from the program being accessed.

Newsguy Free Trial Accounts increased to 20 GB

Re: exec php

Quoted text here. Click to load it
The thing is that the php function is expecting something back. Thus
getting the result of the exec command is needed by the php script.
About the bad combination: The site is running windows media server,
so I cannot go to linux. And I cannot recreate a whole site, a big
one, in asp.net!

Re: exec php

On Dec 21, 8:01 am, wart...@hotmail.com wrote:
Quoted text here. Click to load it

First, what kind of program are you calling?  Whats it do?
Second, please tell me you looked up Exec on the php.net site and read
all the comments made by folks who have done or attempted to do what
you are trying.  The answer might be right there!
Third, what kind of resources does the called program use?  Are you
running out of memory?  are you running out of connections to a
database?  What is your take on why the program is failing?  What
permissions does the called program have?  What permsissions does it

Re: exec php

Quoted text here. Click to load it
Its a few programs, made in c#. The programs mostly make operations on
the windows media server. They create publishing point, for example,
on the fly.

Quoted text here. Click to load it

I had a look there. But no one that got the same king of problem.

Quoted text here. Click to load it

I put log functions both in the php-script and in the c# app.

I restart the server, and everything works. But then, after may one
day, for some reason, one of the c# app (can be any of them) freezes
during the execution, and before it can returns.

The php-log says that, well, nothing. It stops when it has to show the
return value got from the c# app.

The C# app doesnt catch. That is the big problem. The log function in
the c# app is located at the end of the try statement. And exactly
before the return statement. In the log file I try to write the return
value. But it is blank, for some reason.

Worst of all, maybe, is that the c# app is runnable from the command
line. Always. And it executes returning the value. So the problem, as
I see it, is that suddenly the php-interpretor hangs up when running

Yes, this is obvious, as I created a dummy c# app, one that only
returns hello world. Well, after the php-interpretor (or is it apache
stuff?) hangs up, I cannot either get the return value from that app.

Is it an apache problem? What do you think guys? But the error log
doesnt have any trace of problems with the exec...

Re: exec php

On Dec 23, 9:35 am, wart...@hotmail.com wrote:
Quoted text here. Click to load it

There could be several reasons for this behaviour which are not at all
obvious in my opinion.  Here is possibility.

It is possible to schedule Firefox to come up on a specific page (this
type of scheduling is non trivial), and then if you have GreaseMonkey
(GM) installed, it would do its thing and then shut down.  For
example, you could monitor a web site in this way.  At the end, GM
should tell the window to close itself, thus ending the scheduled app.

When I first developed this, it worked finen under manual testing,
only FF would ask me each time whether I really wanted to shut down
(there is a preference setting to control this).  When the task was
automated and run under SYSTEM, I would again be asked whether I
really wanted to exit.  Since I could not respond to that dialog
(since it wasn't visible), there was no way for the program to finish,
so it didn't (yet it had run fine till the end).

The moral is that it might be that your program under certain
circumstances pops up a dialog that no one is there to dismiss.  That
could be a possible explanation to what is happening.

Csaba Gabor from Vancouver

Re: exec php

Csaba Gabor said:
Quoted text here. Click to load it

A while back, I had to write some code that called on Perl2exe on the  
server to compile Perl modules. What I ran into was an issue where I had  
to set the paths appropriately on the box by first executing a shell  
script from php that set up the enironment for my command.

Could it be something like that?


Re: exec php

Quoted text here. Click to load it

"something like that" covers a lot of ground.  It could have some
bearing, though it is not obvious.  Firstly, you are saying that the
program does what it should do.  That would imply that the paths/
environment vars are correct.  However, if it affects the cleanup of
the program somehow it could be material.  Also, it might be affected
by the "Allow service to interact with desktop" setting that you can
make on Apache/your web server (under Control Panel \ Services).


Re: exec php

Csaba Gabor said:

Quoted text here. Click to load it

Gotta love the ambiguous errors. I don't think he said it does what it  
should do, though. I know when I ran exec without the paths right, I  
didn't get any server errors either, and I had the same kind of issue  
he's describing.

So it might cover a lot of ground, but it's kinda always better to have  
a bunch of ideas, no matter how much they cover, I tink.


Re: exec php

Quoted text here. Click to load it

Yes, the c# programs do what they have to do. They work! They work 2
hours, 3 hours, maybe 1 day. And then suddenly it stops to work.

It is extrmly frustating. I have tried to put logs both in the php-
script and in the c# app. Nothing. Not an exception. Nothing.

Again: if I have a simple C# app that only have an Hello World, then
the php script, or to be more exact the exec call from the php-script
to the c# app, seems like going in an infinite loop. Even thought
there is no loop there. The exec waits, for ever...

No error in error.log fo r apache. And, no problem in continuing with
running the server. I can always view other pages. The only problem, a
very big problem, is that at a certain point the exec function hangs

Re: exec php

On 24 Dec, 10:51, wart...@hotmail.com wrote:
Quoted text here. Click to load it


Maybe another intresting thing.

The c# app that I try to run after exec begins to freeze, looks
something like that:

static void Main(string[] args){
            tw.WriteLine("Before at " + DateTime.Now);
            Console.Write("error test");
            tw.WriteLine("After at " + DateTime.Now);

In the c# app there is nothing else than this. The funny thing is that
in the log  can see:

Before at (date)
After at (date)

Console.writeline should have been executed right? It is stated before
the second log line...

Re: exec php

I am quite sure that the problem is located in the function exec. At
some point, that function hangs up. Why? Not easy to know. But is it
possible to log it? Ordinary self made log function doesnt give
anything. Apache error log doesnt give anything either. How can I
analyze what is going on with that function?

Re: exec php

warth33@hotmail.com said:
Quoted text here. Click to load it
If they work for a while and then suddenly stop, have you checked the OS  
  error logs? If there is an error happening, it's going to be recorded  
somewhere. It could be something as simple as your app filling up memory  
somehow, or having too many open threads to it from the OS' viewpoint.

With exec, if it goes out and executes successfully once, then it's most  
likely not broken there, and you might need to go deeper to get to any  
kind of issues. Maybe open up the server process list and watch what's  
happening while it runs, if you can.

Good luck, chasing this one is going to cost you a bunch of time, sounds  

Anthony Levensalor

Re: exec php

warth33@hotmail.com wrote:
Quoted text here. Click to load it

The problem isn't in your exec() call.  Rather, (from an earlier post)  
you need to find out why your program is not returning a value.

exec() works quite well.  I've never found a problem in it - although I  
have found problems in my code at times.

As for the Console.writeline - try a C# newsgroup.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Re: exec php

Oh well. In the C# newsgroup no one could help me. They dont seem to
have any idea o nwhat is going on. I tryed to explain the problem as
detailed as possible, but nothing..
Today I tryed something quite basic. It sounded as it was a good
solution, so bad that I hadnt tried it before. Setting time limit. The
thing was that the time limit in php ini is set to 0 on the server.

So I put this in a test script:

echo 'before exec';
log('before exec');
$ret = exec('path_to_exx_file.exe');
echo 'exec returned ' . $ret;
log('exec returned ' . $ret);
catch(Exception $e){
echo 'failed due to: ' . $e;
log('failed due to: ' . $e);

Note that this script was calling a simple Hello world c# program.

But neither this helped me. The script will run for ever.

Is there really not anyone that can help me out, just give me some
good hint of what the problem can be? Arent they any gurus there out??

Re: exec php

warth33@hotmail.com wrote:
Quoted text here. Click to load it

PHP can only time out when it is in control.  Once it passes control off  
to the C# program, PHP can't time out until control is returned.

And there are PHP gurus here.  But this isn't a PHP problem - PHP isn't  
even involved after the call to exec().

But I also sympathize with you.  These things aren't easy to find.

Maybe start a little more basic.  What happens if you just have a C#  
program which does nothing but return?  What if you don't have any  
output from the C# program?

BTW - posting your C# code here won't help.  Most of us won't understand  
it, anyway.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Re: exec php

Quoted text here. Click to load it

Thank for the support Jerry. I was just a little bit frustrated, thats
why I told that think about gurus.
Well I will try to call a c# that doesnt return anything. And I will
see if exec runs forever even in that case. Im just waiting for the
next time the server hangs up, which will happen in the next few hours
or next few days (I think the site web application worked without
hanging up for more than a couple of days). I'll be back soon..

Re: exec php

Here we are. The problem showed up again. The php script looks like

write_to_log("Beginning at " . date('Y-m-d H:i:s'));
$exec_command = 'app.exe';
$str = "Before at " . date('Y-m-d H:i:s');
write_to_log("Before at " . date('Y-m-d H:i:s'));
try {
write_to_log("trying at " . date('Y-m-d H:i:s'));
write_to_log("after execution at " . date('Y-m-d H:i:s'));
write_to_log("Ends at " . date('Y-m-d H:i:s'));
catch (Exception $e) {
write_to_log("message was " . $e->getMessage());

The log file, when everything works, contains this:

Beginning at 2008-01-17 19:02:09
Before at 2008-01-17 19:02:09
trying at 2008-01-17 19:02:09
after execution at 2008-01-17 19:02:09
Ends at 2008-01-17 19:02:09

The log file, when it hangs up, contains this:

Beginning at 2008-01-17 21:10:52
Before at 2008-01-17 21:10:52
trying at 2008-01-17 21:10:52

I used another c# app, a one that doesnt return anything. But the exec
still hangs up. Most people here dont know anything about c#, but
Ipost it cause its very simple stuff, and I think every programmer
should understand it, even if it is a different syntax:

        static void Main(string[] args){
            TextWriter tw = new StreamWriter("C:\logs\app.txt",
            tw.WriteLine("beginning at " + DateTime.Now);
            tw.WriteLine("ending at " + DateTime.Now);

The app is only writing to the log file. And is not printing anything
to the console (which corresponds to the return value that the php
script use to get).

The log file for the c# app, when everything works, looks like this:

beginning at 2008-01-17 21:10:52
ending at 2008-01-17 21:10:52

And, the funny thing, it looks exactly the same, even after the exec
hangs up. The c# is executed, always, when called from php through
php. But, when saying that exec is hanging up, it means that it doesnt
return. Thats all.

Site Timeline