开发者

PHP system() hangs even if the command in system() finishes

I have a code something like this:

$file = fopen( "debug.txt", "w" );
$command = "myExe.exe param0 param1";
fprintf( $file, "starting\r\n" );
fflush( $file );
system( $command );
fprintf( $file, "the end...\r\n" );
fflush( $file );

It prints "starting" but not "the end...". The system() function hangs.

The myExe.exe is an applicatication written in C++, which actually terminates; i.e. the main function of myExe ends with a code like this:

FILE* f = fopen( "test.txt", "w" );
fclose(f);
return 0;

test.txt is created, which means "myExe.exe" works and开发者_C百科 finishes normally.

That problem does not occur each time we call the php file; sometimes hangs, sometimes works...

Any help&idea will be appriciated. Thanks in advance.

info; OS: win xp php server: wamp server 2.0

edit: my problem is not about my debug files. It is about system() or exec() functions. I can remove all other lines.

my php script works well for about 4/5 tries. After system() called, i call some sql functions but when system() hangs, my page will give a fatal error.


There is a known bug in php on windows (http://bugs.php.net/bug.php?id=44942).
There is a workaround you may want to try, close the session (call session_write_close() ) before calling the exec function.


Try using an sprintf to print the formatted text to a string, then a write, like so:

$file = fopen( "debug.txt", "w" );
$command = "myExe.exe param0 param1"; 
$startStr = sprintf( "starting\r\n" );
fwrite($file, $startStr);
fflush( $file );  
system( $command );  
$endStr = sprintf( "the end...\r\n" );
fwrite($file, $endStr);
fflush( $file );  

Or, if you're not using any formatted strings (which in this case it doesn't look like you are), get rid of the sprintfs and just use write.


I would recommend you download/buy something like phpDesigner or RapidPHP so you can step through the logic of the program and see what exactly is going on. Nothing jumps out at me for being wrong with the program but if there is, either of the above programs will find it and display it to you in red.

You really do not need the "\r\n" unless you just like double spacing things. Just the "\n" should work fine.

Also, have you tried the "php -l " command yet to check for errors? It might turn up something.

Last, but not least, there are other commands in PHP to run programs externally - have you tried the other commands yet?

Just some thoughts. :-)

PS: I just had another thought: What are param0 and param1? If they contain special characters - that might influence what is happening with the system command.

PPS: AH! I may have a partial answer. The "return 0" line might be the culprit. Try changing it to "exit( 0 );" instead. The return statement is not the same as exit. It tries to return to the calling program and there is none. However, the system might get confused and think it should return to the PHP script (since all a return command does is a RET which causes the JSR from the system command to try to catch the RET. By replacing it with an exit command you are telling the system command you are through with your program. The system command will then do its own RET command back to PHP. If that makes sense. Basically, you are doing a double RET with the return command and you are also pushing the zero(0) status code onto the stack. Since the system returns its own status normally (via the exit command) what might be happening is that the null (0) is being interpreted as a stop command to the system command. IE: The zero gets popped onto the system stack, the RET is generated, the system pops off the RET leaving the zero(0 or null) byte which it isn't expecting and that freezes the system command. Again, the answer would be to switch to using "exit( 0 );" rather than the return command. Just a guess but I think that I ran into this years ago when I was doing Perl and the answer then was to use exit instead of return. So I'm figuring with PHP it might be the same problem. (Had to think about this for a while before I remembered it.)


I have a feeling, yes just a feeling, that there are open handles in your process preventing it from exiting. Most of the time when this happens it the STDIN and STDOUT handles. Did you check to see if fclose() in your C++ application succeeded?

Download: http://technet.microsoft.com/en-us/sysinternals/bb896653

Use it to see how many handles are open for myEXE.exe when it hangs.

Did you happen to greatly simplify your myEXE.exe for the purposes of SO and missed something?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜