By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,722 Members | 1,174 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,722 IT Pros & Developers. It's quick & easy.

exec() works only sometimes

P: n/a
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
Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Thu, 23 Sep 2004 16:41:39 +0200, "Thomas Mlynarczyk"
<bl*************@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 / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #2

P: n/a
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?

Jul 17 '05 #3

P: n/a
Thomas Mlynarczyk <bl*************@hotmail.com> wrote:
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?


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
Jul 17 '05 #4

P: n/a
Also sprach Simon Stienen:
D:\Apache2\bin>apache.exe -h 2>NUL


What does the "2" in the above mean?


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
Jul 17 '05 #5

P: n/a
Thomas Mlynarczyk <bl*************@hotmail.com> wrote:
Also sprach Simon Stienen:
D:\Apache2\bin>apache.exe -h 2>NUL

What does the "2" in the above mean?


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


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
Jul 17 '05 #6

P: n/a
Also sprach Simon Stienen:
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.


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.
Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.