howto kill proc stream after X seconds

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

 



Hi,

 

I am struggling to find a way to kill a proc_open() stream if it remains
idle (hangs).  Specifically, I am opening an SSH connection up and running a
remote command - all from a PHP script run from the shell.  Under the rare
circumstance that my passwordless authentication fails (ie: it sits there
waiting for a password), I want to be able to 1) check to see if this is the
case 2) kill the stream 3) move on to other tasks.  My "solution" (it
doesn't work) has been to set blocking to false, and check to see if it
continues looping in the fread() for 5 seconds.  If the loop is > 5 seconds,
I try to send a CTRL-C to the opened ssh command, break out of the loop and
move on.  But, the CTRL-C doesn't kill the ssh command, and the script gets
stuck (ie: it doesn't do it's var_export) until I manually CTRL-C from the
shell. looks like this so far:

 

$spec = array(

                        0 => array("pipe", "r"),  

                        1 => array("pipe", "w"),  

                        2 => array("file", "/tmp/error-output.txt", "a") 

                    );

$process = proc_open(''ssh somehost someremotecommand';', $spec, $pipes);

 

if (is_resource($process)) {

    stream_set_blocking($pipes[1], false);

    stream_set_blocking($pipes[0], false);

    

    $contents = '';

    $time = time();

    while (!feof($pipes[1])) {

        $contents .= fread($pipes[1], 8192);

        if (time() - $time > 5) {

            // send ctrl-c

fwrite($pipes[0], chr(4));

            break;

        }

    }

 

    fclose($pipes[0]);

    fclose($pipes[1]);

    

    $return_value = proc_close($process);

}

 

var_export($contents);

 

die();

 

 

Any ideas on how I can do this?




CONFIDENTIALITY NOTICE
This message contains confidential information intended only for the use of
the individual or entity named as recipient. Any dissemination, distribution
or copying of this communication by anyone other than the intended recipient
is strictly prohibited. If you have received this message in error, please
immediately notify us and delete your copy. Thank you.

AVIS DE CONFIDENTIALITÉ
Les informations contenues aux présentes sont de nature privilégiée et
confidentielle. Elles ne peuvent être utilisées que par la personne ou
l'entité dont le nom paraît comme destinataire. Si le lecteur du présent
message n'est pas le destinataire prévu, il est par les présentes prié de
noter qu'il est strictement interdit de divulguer, de distribuer ou de
copier ce message. Si ce message vous a été transmis par mégarde, veuillez
nous en aviser immédiatement et supprimer votre copie. Merci.


[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