Inserting delay in PHP scripts

Reply

Join Date: Jun 2008
Posts: 21
Reputation: gargg321 is an unknown quantity at this point 
Solved Threads: 0
gargg321 gargg321 is offline Offline
Newbie Poster

Inserting delay in PHP scripts

 
0
  #1
Sep 26th, 2008
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:-
  1. <html>
  2. <body>
  3. <?php
  4.  
  5. $cpu=exec('top -d 1 -n 1|grep Cpu | tail -n 1 | cut -c 25-33');
  6. echo "The Current CPU Utilization is ".$cpu;
  7. <?php
  8. $mem=exec('top -n 1|grep Mem');
  9. echo $mem;
  10. ?>
  11. </body>
  12. </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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,449
Reputation: cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about 
Solved Threads: 135
cwarn23's Avatar
cwarn23 cwarn23 is offline Offline
Nearly a Posting Virtuoso

Re: Inserting delay in PHP scripts

 
0
  #2
Sep 26th, 2008
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).
  1. <html>
  2. <body>
  3. <?php
  4. $oldseconds=date(s);
  5. if ($oldseconds==58)
  6. {
  7. $newseconds=='00';
  8. } else
  9. if ($oldseconds==59)
  10. {
  11. $newseconds=='01';
  12. } else
  13. {
  14. $newseconds=$oldseconds+2;
  15. }
  16. while (date(s)!=$newseconds)
  17. {
  18. //anything before this loop may not be delayed
  19. //anything after this loop will be delayed.
  20. // But with some browsers or servers this
  21. // loop will delay the entire page
  22. }
  23. //place below here what you want after 2 seconds
  24. $cpu=exec('top -d 1 -n 1|grep Cpu | tail -n 1 | cut -c 25-33');
  25. echo "The Current CPU Utilization is ".$cpu;
  26. <?php
  27. $mem=exec('top -n 1|grep Mem');
  28. echo $mem;
  29. ?>
  30. </body>
  31. </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
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 849
Reputation: R0bb0b is on a distinguished road 
Solved Threads: 67
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Practically a Posting Shark

Re: Inserting delay in PHP scripts

 
0
  #3
Sep 26th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,075
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: Inserting delay in PHP scripts

 
0
  #4
Sep 27th, 2008
Originally Posted by cwarn23 View Post
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).
  1. <html>
  2. <body>
  3. <?php
  4. $oldseconds=date(s);
  5. if ($oldseconds==58)
  6. {
  7. $newseconds=='00';
  8. } else
  9. if ($oldseconds==59)
  10. {
  11. $newseconds=='01';
  12. } else
  13. {
  14. $newseconds=$oldseconds+2;
  15. }
  16. while (date(s)!=$newseconds)
  17. {
  18. //anything before this loop may not be delayed
  19. //anything after this loop will be delayed.
  20. // But with some browsers or servers this
  21. // loop will delay the entire page
  22. }
  23. //place below here what you want after 2 seconds
  24. $cpu=exec('top -d 1 -n 1|grep Cpu | tail -n 1 | cut -c 25-33');
  25. echo "The Current CPU Utilization is ".$cpu;
  26. <?php
  27. $mem=exec('top -n 1|grep Mem');
  28. echo $mem;
  29. ?>
  30. </body>
  31. </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:

  1. echo "The Current CPU Utilization is ".$cpu;
  2. <?php
  3. $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.

  1. if (strlen($content) < 256) {
  2. $content = str_pad($content, 256); // IE hack
  3. }
  4. echo $content;
  5. @ob_flush();
  6. 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!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC