| | |
Inserting delay in PHP scripts
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jun 2008
Posts: 21
Reputation:
Solved Threads: 0
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:-
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:-
PHP Syntax (Toggle Plain Text)
<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>
Commitment is what transforms a promise into reality. It is the words that speak boldly of your intentions. And the actions which speak louder than the words. It is making the time when there is none. Commitment is the stuff character is made of; the power to change the face of things. It is the daily triumph of integrity over skepticism.
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).
php Syntax (Toggle Plain Text)
<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>
Try not to bump 10 year old threads as it can be really annoying.
Like php then read my website at http://syntax.cwarn23.net/
Star-Trek-Atlantis - now that's what I call a movie ^_^
My favourite PC. - MacGyver Fan
Bad english note: dis-iz-2b4u
Like php then read my website at http://syntax.cwarn23.net/
Star-Trek-Atlantis - now that's what I call a movie ^_^
My favourite PC. - MacGyver Fan
Bad english note: dis-iz-2b4u
you can also use sleep
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss
-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
•
•
•
•
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).
php Syntax (Toggle Plain Text)
<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>
PHP should wait for the exec() command to complete, unless you specified a background process with & in the command.
Remove the <?php in:
PHP Syntax (Toggle Plain Text)
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.
PHP Syntax (Toggle Plain Text)
if (strlen($content) < 256) { $content = str_pad($content, 256); // IE hack } echo $content; @ob_flush(); flush();
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
![]() |
Other Threads in the PHP Forum
- Previous Thread: PHP Web Shell
- Next Thread: I have problem with two value search
| Thread Tools | Search this Thread |
apache api array beginner binary body broken buttons cakephp checkbox class cms code cron curl database date date/time display dynamic ebooks echo email error file files folder form forms function functions global google href htaccess html image include insert ip javascript joomla limit link list login mail mediawiki menu mlm msqli_multi_query multiple mycodeisbad mysql number oop parameter paypal pdf php phpincludeissue problem query radio random recourse recursion regex remote script search seo server sessions sms source sp space speed sql static subdomain syntax system table tag tutorial update upload url validator variable vbulletin video web webdesign white wordpress xml youtube






