PHP output to command line - php

I start my script from command line and it outputs things as they happen but a week ago it stopped outputing and now outputs everything when script finishes. I have ob_start() but as I know this does not effect command line output.

An easy way to do it is to create a function in php like this:
function console_log($message) {
$STDERR = fopen("php://stderr", "w");
fwrite($STDERR, "\n".$message."\n\n");
fclose($STDERR);
}
where $message is the desired output to command line. Then simply call the function wherever you would like to output and pass in whatever you want it to print.

You need to remove ob_start()... try this code on the command line, and it will print the text all at once:
<?
ob_start();
echo "test\n";
sleep(10);
echo "buffer\n";
?>

It'd be very helpful if you could post your script here, at least the relevant parts, but things I'd test are:
Did you turn on buffering?
Are you running the process in something like a nohup or something else that may be buffering it?
Did you change any other buffering settings?
Outputting only at the end of the script seems a buffering problem.

Related

How to echo text before shell_exec in PHP

Kindly please help me with this PHP problem. PHP file will call python file according to the user request. This python is carrying a task. I want to echo text before execute the python file. Because I want PHP to give alert on what it is going to do.
for your information, i tried a dummy file of python. the code for python is just to sleep(10);
for PHP code, I have tried to use ob_flush;, ob_start(); and all. the code is as following. as for reminder. the algorithm.py is only contain 10second sleep.:
ob_start();
echo "welcome";
ob_flush();
usleep(1);
$output=shell_exec("./$algorithm.py");
even with this code, it only echo text after finishes shell_exec. I have tried to use exec. still the text display is delayed.
Output Buffering can be used.
ob_implicit_flush(true)
ob_start();
echo('Task is being done');
ob_flush();
$output=shell_exec("./$algorithm.py");
echo('Wait...');
ob_flush();
echo('done.');
ob_end_flush();
echo "welcome"."<pre>".$output."</pre>";

Store PHP Output To File?

I'm working on a cron php script which will run once a day. Because it runs this way, the output from the file can't be seen.
I could literally write all the messages I want into a variable, appending constantly information I want to be written to file, but this would be very tedious and I have a hunch not necessary.
Is there a PHP command to tell the write buffer to write to a log file somewhere? Is there a way to get access to what has been sent to the buffer already so that I can see the messages my script makes.
For example lets say the script says
PHP:
<?
echo 'hello there';
echo 'hello world';
?>
It should output to a file saying: 'hello therehello world';
Any ideas? Is this possible?
I'm already aware of
file_put_contents('log.txt', 'some data', FILE_APPEND);
This is dependent upon 'some data', when I don't know what 'some data' is unless I put it in a variable. I'm trying to catch the results of whatever PHP has outputted.
You may want to redirect your output in crontab:
php /path/to/php/file.php >> log.txt
Or use PHP with, for example, file_put_contents():
file_put_contents('log.txt', 'some data', FILE_APPEND);
If you want to capture all PHP output, then use ob_ function, like:
ob_start();
/*
We're doing stuff..
stuff
...
and again
*/
$content = ob_get_contents();
ob_end_clean(); //here, output is cleaned. You may want to flush it with ob_end_flush()
file_put_contents('log.txt', $content, FILE_APPEND);
you can use ob_start() to store script output into buffer. See php documentation ob_get_clean
<?php
ob_start();
echo "Hello World";
$out = ob_get_clean();
$out = strtolower($out);
var_dump($out);
?>
If You're using cron I suppose that You run this on a Unix machine so:
One of approach is to write everything You want to stdout stream so in Unix You may grab this output to a file:
in php script:
$handle = fopen("php://stdout","w");
fwrite($handle,"Hello world"); // Hello world will be written to console
in cron job grab this output to a file:
#hourly php /var/www/phpscript.php >> /path/to/your/outputfile.txt
Notice: >> operator will append to a file and > operator will overwrite file by new data. File will be created automatically by first write
So everything you put to fwrite call as second argument will be placed in /path/to/your/outputfile.txt
You may call fwrite as many time as you want. Don't forget to close handler by fclose($handle);

Compile a .php file which takes in 4 parameter from another php file?

I have a script which when compiled from the terminal passing the parameters something like this "php compile.php arg1 arg2 arg3 arg4" on the terminal starts displaying the output result which is something like 1)file created 2) file inclusion. etc.
Now I am trying to use system(php compile.php arg1 arg2 arg3 arg4) from another php file, but it will not execute the php file or will not show an output.
For example i even tried to create hello.php------
and tried using system(php hello.php); but it did not output anything on the browser.
Can anyone please help me out, I am new to php. Thanks.
make sure you react on errors from the system call. Your actual code might also help. The following example works without problems on my box.
<?php
$file = fopen('/tmp/written.php', 'w');
fwrite($file, '<?php echo "Hello from written.php\n"; ?>');
fclose($file);
echo "calling written.php\n";
if (!system('php /tmp/written.php'))
die("something wrong\n");
else
exit("all good\n");
?>

Show output taken from shell_exec and display it in real time instead of after waiting 5-7min

Right now, I have code as follows.
$output = shell_exec( !-- unix commands are here --! );
echo $output;
I have a website where, upon the clicking of a particular button, the shell script is outputted and it is displayed on the browser. This is working perfectly. The only issue is that I can't see what's happening with the output until it is finished. I have to wait about 5-7 minutes, and then I see about a hundred lines of output. I am trying to push the output to the browser as the output executes -- I want to be able to see the output as its happening in real time (on the browser).
I've tried to use popen, proc_open, flush(), ob_start, etc. Nothing seems to be working. I just tried opening a text file, writing the contents of the output to the textfile, and reading the textfile incrementally on a loop. I'm a php beginner so it's possible that I haven't been using any of the above methods properly.
What is the simplest way to accomplish this?
Because PHP runs exec, system, pass_thru, etc in blocking mode, you are very limited in possibilities. PHP will require the code to finish executing before moving on throughout the script, unless you do something like add the following to your command:
"> /dev/null 2>/dev/null &"
Of course, this will halt the output of your command, but.. maybe something like:
exec('command > /cmd_file 2>/cmd_file &');
$file = fopen('/cmd_file', 'r');
while (!feof($file)) {
echo fgets($file);
sleep(1);
}
fclose($file);
Worth a shot.

php - output echo'd contents in browser while script is running?

Does anyone know how to output stuff I echo to a browser while the script is running?
I have a long loop that I'd like to output a string after every run of the loop, kind of like a progress bar, does anyone know how to do that?
Use the ob_flush() function:
ob_start();
//echo stuff...
ob_flush();
//echo more stuff...
ob_flush();

Categories