Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
php script to read and send a stream of chars to remote clie
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
Vieri
Guru
Guru


Joined: 18 Dec 2005
Posts: 515

PostPosted: Tue Sep 18, 2018 9:00 am    Post subject: php script to read and send a stream of chars to remote clie Reply with quote

Hi,

Please consider the following simple scenario.
In one Gentoo console (console 1) I run these commands:

Code:
# echo "Line1" >> /tmp/test.txt
# echo "Line2 OPTION and HTTP and more." >> /tmp/test.txt


and in another console (console 2) I run the following at the same time:

Code:
# tail -n 0 -f /tmp/test.txt | grep -E '.*OPTION.*HTTP.*'
Line2 OPTION and HTTP and more.


Of course, the latter filtered result is expected.

Now, I'd like to do the same, but via HTTP.

So I keep running the following in console 1 over and over:

Code:
# echo "Line2 OPTION and HTTP and more." >> /tmp/test.txt


I set up a PHP script as so:

Code:
# cat test_popen.php
<?php
if ($_SERVER['REQUEST_METHOD']=='POST') {
  $input = filter_input_array(INPUT_POST);
} else {
  $input = filter_input_array(INPUT_GET);
}

if ($input['action'] === 'view') {
    header('Content-Type: text/html; charset=utf-8');
    $cmd_run = "tail -n 0 -f /tmp/test.txt";
    if (strlen($input['filter']) > 0)
        $cmd_run .= " | grep -E '".$input['filter']."'";
    echo "Starting CMD=$cmd_run\n";
    ob_flush();
    flush();
    $po = popen($cmd_run, "r");
    while ($l = fgets($po, 32)) {
        echo $l;
        ob_flush();
        flush();
        }
    pclose($po);
    }
?>


and in console 2 I run this HTTP client:

Code:
# curl 'http://localhost/test/test_popen.php?action=view&filter=.*OPTION.*HTTP.*'
Starting CMD=tail -n 0 -f /tmp/test.txt | grep -E '.*OPTION.*HTTP.*'


That's all I see despite the fact that I'm still running the commands in console 1.

The only way I can get any output is if I run this in console 2:

Code:
# curl 'http://localhost/test/test_popen.php?action=view'


So I guess my mistake must be with the | pipe right before grepping and calling popen on that.
How can I fix this?
The goal is to continuously send a filtered log file via HTTP.

Thanks
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum