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

From: Date: Tue, 28 Nov 2006 11:48:07 +0000
Subject: Re: CLI script & exec() - how to control what gets dumped on the screen.
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-245222@lists.php.net to get a copy of this message
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

« previous php.general (#245222) next »