For Programmers: Free Programming Magazines  


Home > Archive > PHP Programming > September 2004 > exec() works only sometimes









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author exec() works only sometimes
Thomas Mlynarczyk

2004-09-23, 3:56 pm

Hello!

I run PHP 4.3 as an apache module. I use apache 1.3 as a service under
WinXP. And I've got a strange problem with exec():

The following commands are executed correctly:
c:\windows\system32\cmd.exe /?
c:\apache\apache.exe -w -n "Apache" -k restart

The following commands are not executed (empty output array, return code =
1):
c:\Apache\Apache.exe -h
c:\windows\system32\cmd.exe /C c:\apache\apache.exe -h

It works with the console, however. So why not with PHP?

Can anyone help me?
Thomas


Andy Hassall

2004-09-24, 8:56 pm

On Thu, 23 Sep 2004 16:41:39 +0200, "Thomas Mlynarczyk"
<blue_elephant55@hotmail.com> wrote:

>I run PHP 4.3 as an apache module. I use apache 1.3 as a service under
>WinXP. And I've got a strange problem with exec():
>
>The following commands are executed correctly:
>c:\windows\system32\cmd.exe /?
>c:\apache\apache.exe -w -n "Apache" -k restart
>
>The following commands are not executed (empty output array, return code =
>1):
>c:\Apache\Apache.exe -h
>c:\windows\system32\cmd.exe /C c:\apache\apache.exe -h
>
>It works with the console, however. So why not with PHP?


This is in fact consistent - the commands ARE being run. The convention for
'usage' commands is to output the usage information on standard error and NOT
standard output, and to exit with a non-zero (failure) error code.

D:\Apache2\bin>apache.exe -h
Usage: apache.exe [-D name] [-d directory] [-f file]
[-C "directive"] [-c "directive"]
[-w] [-k start|restart|stop|shutdown]
[-k install|config|uninstall] [-n service_name]
[-v] [-V] [-h] [-l] [-L] [-t] [-S]
Options:
-D name : define a name for use in <IfDefine name> directives
[ ... snip ... ]
-t : run syntax check for config files

D:\Apache2\bin>echo %ERRORLEVEL%
1

There's your '1' return code.

D:\Apache2\bin>apache.exe -h 2>NUL

D:\Apache2\bin>

Redirecting standard error shows that there's nothing sent to standard output.

--
Andy Hassall / <andy@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Thomas Mlynarczyk

2004-09-25, 8:59 am

Also sprach Andy Hassall:

[some exec() commands work, others not]

> This is in fact consistent - the commands ARE being run. The
> convention for 'usage' commands is to output the usage information on
> standard error and NOT standard output, and to exit with a non-zero
> (failure) error code.


Thanks for shedding some light on this mystery.

> D:\Apache2\bin>apache.exe -h 2>NUL


What does the "2" in the above mean?





Simon Stienen

2004-09-25, 8:59 am

Thomas Mlynarczyk <blue_elephant55@hotmail.com> wrote:
> Also sprach Andy Hassall:
>
> [some exec() commands work, others not]
>
>
> Thanks for shedding some light on this mystery.
>
>
> What does the "2" in the above mean?


It's the standard stream constant iirc:
0 - STDIN
1 - STDOUT
2 - STDERR
--
Simon Stienen <http://dangerouscat.net> <http://slashlife.de>
»What you do in this world is a matter of no consequence,
The question is, what can you make people believe that you have done.«
-- Sherlock Holmes in "A Study in Scarlet" by Sir Arthur Conan Doyle
Thomas Mlynarczyk

2004-09-26, 9:06 am

Also sprach Simon Stienen:

>
> It's the standard stream constant iirc:
> 0 - STDIN
> 1 - STDOUT
> 2 - STDERR


I tried it appending 2>&1 to the command (had not tried that before as I
thought this was *nix only) and bingo! It works! Only thing I wonder: If
Apache is supposed to return an error code of 0 if all is OK, how can I find
out about it?

Thanks again to all of you!
Thomas


Simon Stienen

2004-09-26, 9:06 am

Thomas Mlynarczyk <blue_elephant55@hotmail.com> wrote:
> Also sprach Simon Stienen:
>
>
> I tried it appending 2>&1 to the command (had not tried that before as I
> thought this was *nix only) and bingo! It works! Only thing I wonder: If
> apache is supposed to return an error code of 0 if all is OK, how can I find
> out about it?
>
> Thanks again to all of you!
> Thomas


http://de3.php.net/function.exec states:
| string exec ( string command [, array &output [, int &return_var]])
^^^^^^^^^^^^^^^
Saying in the function description:
| If the return_var argument is present along with the output argument,
| then the return status of the executed command will be written to this
| variable.

HTH
Simon
--
Simon Stienen <http://dangerouscat.net> <http://slashlife.de>
»What you do in this world is a matter of no consequence,
The question is, what can you make people believe that you have done.«
-- Sherlock Holmes in "A Study in Scarlet" by Sir Arthur Conan Doyle
Thomas Mlynarczyk

2004-09-26, 8:55 pm

Also sprach Simon Stienen:
[color=darkred]
> http://de3.php.net/function.exec states:
> ^^^^^^^^^^^^^^^
> Saying in the function description:

Well, I use the exec() with both $output array and $return variable. And the
latter contains 1, even though the command is supposed to have been
successful.


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com