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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



if i understand correctly, you want to run a system cmd from inside a php
app. you also want to return/display the result of the system action.

here's a sample of what i use in a php/cli test app. if i recall, i've done
the same thing in php web apps as well..

        $rm_conf = "rm -f /svntest/%s/conf/svnserve.conf";
        $symlink = "ln -s %s/svnserve.conf /svntest/%s/conf/svnserve.conf";

        $rm_conf1 = sprintf($rm_conf, $collegerepos);
        $symlink1 = sprintf($symlink, $col_base, $collegerepos);
        $res = system($rm_conf1, $rval);
        if($rval == 0)
        {
                print "successful remove ".$collegerepos." svnserve.conf\n";
        }
        else
        {
                print " screwed up remove svnserve.conf -- ".$res. "\n";
                exit();
        }

the $res is the resulting output of the cmd, the $rval is the returned err
val of the cmd...



-----Original Message-----
From: Stut [mailto:stuttle@xxxxxxxxx]
Sent: Tuesday, November 28, 2006 3:40 AM
To: Jochem Maas
Cc: [php] PHP General List
Subject: Re:  CLI script & exec() - how to control what gets dumped
on the screen.


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...

    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.

-Stut

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux