7

I want to write a small management tool to oversee my server processes, my problem is now, how could i wait for user input and at same time update the screen with current stats? Is it even possible with PHP-CLI or are there any tricks for doing this I missing currently?

I have looked into newt and ncurses PECL extensions but both doesn't seem to fit my needs.

hakre
  • 193,403
  • 52
  • 435
  • 836
  • 1
    I would suggest to use Bash for this task. – Roman Newaza May 15 '12 at 05:11
  • 1
    I think you might be able to use this: http://stackoverflow.com/questions/3684367/php-cli-how-to-read-a-single-character-of-input-from-the-tty-without-waiting-f – h00ligan May 15 '12 at 07:29
  • @h00ligan thx for that, but they extensivly use system/exec so, in this solution there is no need to even use php. like roman-newaza suggested. But the Trick with the fread while loop is pretty cool. – Tobias Herkula May 15 '12 at 14:58
  • Your question is really blured . Please review and edit it. Go ahead and google about server sent evets or websockets , but im 90% sure that this might not be what ur looking for. – Cata Cata May 21 '12 at 06:32
  • @CataCata What have Server Events or Websockets to do with a Commandline Tool? I don't even have a Webserver running on the box where I need to monitor process... – Tobias Herkula May 21 '12 at 17:58
  • @TobiasHerkula Then why are u using php ( server side language ) , if u don't have a server ? Why would you need php on ur box to read some packets , data ? i still don't get what ur question is about. – Cata Cata May 21 '12 at 21:33
  • @CataCata Do you know what CLI means? The only way for me to connect to these boxes are through ssh and they don't run any unnecessary services like an httpd. They are worker for a queue endpoint and sometimes the worker services fail and I wanted to write a little tool to monitor these workers and control (init.d stuff) them with a nice little CLI GUI. – Tobias Herkula May 22 '12 at 02:05

4 Answers4

1

Go for libevent http://www.php.net/manual/en/book.libevent.php

You can run your main loop while listening to console with a code roughly like this one:

<?php   
// you need libevent, installable via PEAR
$forever=true;
$base=event_base_new();
$console=event_buffer_new(STDIN,"process_console");
event_buffer_base_set($console,$base);
event_buffer_enable($console,EV_READ);
while ($forever) {
    event_base_loop($base,EVLOOP_NONBLOCK); // Non blocking poll to console listener
    //Do your video update process
}
event_base_free($base); //Cleanup
function process_console($buffer,$id) {
    global $base;
    global $forever;
    $message='';
    while ($read = event_buffer_read($buffer, 256)) {
        $message.=$read;
    }
    $message=trim($message);
    print("[$message]\n");
    if ($message=="quit") {
        event_base_loopexit($base);
        $forever=false;
    }
    else {
        //whatever.....
    }
}
Alar
  • 760
  • 4
  • 11
0

I don't think you can do it with PHP CLI. As I know, when interpret the script with PHP, you can only view the final output.

Hieu Le
  • 8,288
  • 1
  • 34
  • 55
0

I think you do want ncurses. If you can convert the simple example C code here, which you should be able to with the PHP wrapper, you'd have your "bootstrap" for solving your problem.

Make sure to blog your code somewhere! :)

ZagNut
  • 1,431
  • 15
  • 20
-1

My advice would be to try to avoid any solutions that talk about leaving processes running whilst exiting PHP. Here is a really simple example of how to do it with a bit of jQuery:

window.setInterval(checkstat, 10000); //10 second interval

function checkstat() {

    //Change a div with id stat to show updating (don't need this but it's nice)
    $('#stat').html('Updating...'); 

    $.get('/getmystats.php?option=blah', function(data) {
        //Update the results when the data is returned.
        $('#stat').html(data);
    });

} 

If you are need to update more than one area on your page, you can do one call but return JSON or XML and then populate the bits as required.

Enigma Plus
  • 1,519
  • 22
  • 33