0

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?

6
  • crontab instead of infinite loop? using a capped collection in mongodb might help you getting around the problem of the output file growing too large Commented Jul 19, 2013 at 13:51
  • 1
    I'm thinking some kind of asynchronous technology like ajax or node.js might work. The browser could wait for a response from the PHP script, then when it receives one, show it on the screen with javascript or jQuery, then immediately start waiting for another. Are these lines of data being fed into a database such as MySQL? Might need to put the data into a database temporarily then remove the rows from the db once they've been sent to a browser. Commented Jul 19, 2013 at 13:57
  • No unfortunately it is infinite loop by design. Besides, each 'sweep' of the loop would result in a file of several MB. What I really want is a semi-live view of what is going on. Commented Jul 19, 2013 at 13:58
  • @trpt4him the main command-line PHP script is running in a location outside of the web root, so even with AJAX I'm not sure how it would interface with it? The script does write results to a DB, but its the stuff it doesn't write that I'm interested in. Commented Jul 19, 2013 at 14:00
  • I'm sorry to say, but infinite loop smells like bad design to me. I'm with @trpt4him: node.js would be a good fit, thanks to its non-blocking IO, it'd allow you to access its output while the script is running. It's also great at idling, so you don't need an infinite loop construct, but could work with events Commented Jul 19, 2013 at 14:13

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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' );

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.