Re: [PHP] CLI script & exec() - how to control what gets dumped on the screen.



Stut wrote:
Jochem Maas wrote:
I have been trying to figure out how to use exec to run some
commandline scripts (some perl scripts & mysql in this case) WITH the
output of the commands run via exec being shown on the screen.

neither of these examples have the desired effect:

$output = array(); $exit = 0; exec('apache2ctl graceful', $output,
$exit);


$output = array(); $exit = 0; @exec('apache2ctl graceful', $output,
$exit);

please someone tell me there is a decent way to do this (something to
do with STD input/output redirection or something?) and that I'm not
left with trying something like:

ob_start(); $output = array(); $exit1 = $exit2 = 0; @exec('apache2ctl
graceful', $output, $exit1); ob_end_clean();

... of which I don't even know if it would work.

anyone care to share their clue?

Chances are that the output is being sent to stderr instead of stdout.
You need to redirect stderr output to stdout in the command...

you are absolutely right - I fnid it strange that so many commandline apps
stick their 'normal' output to stderr (like I said to Edward: "how is
'Syntax OK' in any way to be considered an error?").

oh well at least now I no how to handle the issue - and I'll be sticking rhe
'2>&1' after ever command sent to the shell via exec() from now on :-)

thanks for your help.


exec('apache2ctl graceful 2>&1', $output, $exit);

Also, in my experience it's better to provide the full path to anything
you shell out to from PHP, especially if it's going to be executed from
a web request.

I just read the same advice in the manual. :-)

but there no web-request involved here (and I want to keep the script as portable as poss.)
it's actually a custom CMS installer for a mini CMS thang I wrote ... (it needs
to restart the webserver after an install is complete)



-Stut
.



Relevant Pages

  • Re: Windows new user cheat sheet?
    ... commandline in connection with ruby. ... associates scripts with ruby so all I need is to write the script in ... in the top left of an open Explorer window) and choose "irb here!" ...
    (comp.lang.ruby)
  • Re: Sending X server a command
    ... Within nvidia-settings, you can do this, but I would ... like to be able to do it from command line as I have some scripts I am ... Commandline Interface ...
    (alt.os.linux)
  • using IPC::Open3 to write to *and* read from a process...
    ... I'm currently working on some cluster scripts which, among other things, ... output on STDOUT or STDERR, and if I can write to the process on STDIN ... I get all the output from the child process? ... while($_ = shift) { ...
    (comp.lang.perl.misc)
  • [Fwd: Cron <operator@balrog> /usr/libexec/save-entropy]
    ... I just got a FreeBSD 5.4-STABLE box up and running, ... all my rc scripts output the ... following to stderr when running: ... Paul Schiro ...
    (freebsd-questions)
  • Re: CLI script & exec() - how to control what gets dumped on the screen.
    ... programs seem to happily spit out their output to stderr (e.g. apache2ctl, ... scripts WITH the output of ... run via exec being shown on the screen. ... @exec('apache2ctl graceful', $output, $exit); ...
    (php.general)