Hi all,

I am writing a PHP script that executes shell commands on a linux machine. When I run this script on Linux system, it waits while it executes the command and store the result in a PHP variable. But when I run it on a web browser, the browser don't wait to get result of the command execution and moves on. I want that browser to wait 2 seconds while it executes that command. How can I do this. Please help me.

The PHP Code is as under:-

<html>
<body>
<?php

$cpu=exec('top -d 1 -n 1|grep Cpu | tail -n 1 | cut -c 25-33');
echo "The Current CPU Utilization is ".$cpu;
<?php
$mem=exec('top -n 1|grep Mem');
echo $mem;
?>
</body>
</html>

Below is a script I made which will delay the page by 2 seconds however, although the page will be processed with the delay, you may find that the whole page may display at the same time (after 2 seconds).

<html>
<body>
<?php
$oldseconds=date(s);
if ($oldseconds==58)
	{
	$newseconds=='00';
	} else
if ($oldseconds==59)
	{
	$newseconds=='01';
	} else
	{
	$newseconds=$oldseconds+2;
	}
	while (date(s)!=$newseconds)
		{
		//anything before this loop may not be delayed
		//anything after this loop will be delayed.
		// But with some browsers or servers this
		// loop will delay the entire page
		}
//place below here what you want after 2 seconds
$cpu=exec('top -d 1 -n 1|grep Cpu | tail -n 1 | cut -c 25-33');
echo "The Current CPU Utilization is ".$cpu;
<?php
$mem=exec('top -n 1|grep Mem');
echo $mem;
?>
</body>
</html>

Below is a script I made which will delay the page by 2 seconds however, although the page will be processed with the delay, you may find that the whole page may display at the same time (after 2 seconds).

<html>
<body>
<?php
$oldseconds=date(s);
if ($oldseconds==58)
	{
	$newseconds=='00';
	} else
if ($oldseconds==59)
	{
	$newseconds=='01';
	} else
	{
	$newseconds=$oldseconds+2;
	}
	while (date(s)!=$newseconds)
		{
		//anything before this loop may not be delayed
		//anything after this loop will be delayed.
		// But with some browsers or servers this
		// loop will delay the entire page
		}
//place below here what you want after 2 seconds
$cpu=exec('top -d 1 -n 1|grep Cpu | tail -n 1 | cut -c 25-33');
echo "The Current CPU Utilization is ".$cpu;
<?php
$mem=exec('top -n 1|grep Mem');
echo $mem;
?>
</body>
</html>

It looks like you have a syntax error in your PHP. That is probably why it is returning immediately.

PHP should wait for the exec() command to complete, unless you specified a background process with & in the command.

Remove the <?php in:

echo "The Current CPU Utilization is ".$cpu;
<?php
$mem=exec('top -n 1|grep Mem');

ps:

Output can also be buffered by PHP and/or the sever. Use ob_flush() and flush() to flush the buffers.
IE6 also doesn't render any output until it receives 256 bytes.

You should make sure you have 256 bytes if you want to display in IE6.

if (strlen($content) < 256) {  
    $content = str_pad($content, 256); // IE hack  
}  
echo $content;
@ob_flush();
flush();
This article has been dead for over six months. Start a new discussion instead.