James
James

Reputation: 666

Make output of cli based PHP script viewable from web without piping to a file?

I have a command line PHP script that runs constantly (infinite loop) on my server in a 'screen' session. The PHP script outputs various lines of data using echo.

What I would like to do is create a PHP web script to interface the command line script so that I can view the echo output without having to SSH into the server.

I had considered writing/piping all of the echo statements to a text file, and then having the web script read the text file. The problem here is that the text file will grow to several megabytes in the space of only a few minutes.

Does anyone know of a more elegant solution?

Upvotes: 0

Views: 256

Answers (3)

Marek
Marek

Reputation: 7423

I think expect_popen will work for you, if you have it available.

Another option is to used named pipes - no disk usage, the reading end has output available as it comes.

Upvotes: 1

hendr1x
hendr1x

Reputation: 1501

As Elias Van Ootegem suggested, I would definitely recommend a cron instead of an constantly running script.

If you want to view the data from a web script you can do a few things....one is write the data to a log file or a database so you can pull it out later....I would consider limiting what you output if you there is so much data (if that is a possiblity).

I have a lot of crons email me data, not sure if that would work for you but I figured I would mention it.

The most elegant suggestion I can think of is to run the commands using exec in a web script which will directly output to the browse if you use : http://php.net/manual/en/function.flush.php

Upvotes: 0

David Normington
David Normington

Reputation: 847

The CLI script can write to a file like so:

file_put_contents( '/var/log/cli-log-'.date('YmdHi').'.log', $data );

Thereby a new log file being created every minute to keep the file size down. You can then clean up the directory at that point, deleting previous log files or moving them or whatever you want to do.

Then the web script can read from the current log file like so:

$log = file_get_contents( '/var/log/cli-log-'.date('YmdHi').'.log' );

Upvotes: 0

Related Questions