Web Design and Web Development Forum

  1. #1
    iMatt's Avatar
    Join Date
    Jun 2005
    Location
    New Jersey
    Age
    21
    Posts
    853
    Rep Power
    10
  2. iMatt is just really nice iMatt is just really nice iMatt is just really nice iMatt is just really nice
  3. Uptime command

    I was just told this from my host as a reply to a question I asked them, I think it's a good response, except I don't know how to do it.

    "Your best bet would be to create a script that runs the "uptime" command every 5-10 minutes, and then pipe the output to a webpage."

    How would I do this?

    P.S.- I sure wish I could offer some sort of points for a good response to this! ;)
    Reply With Quote Reply With Quote
  4. #2
    Join Date
    Mar 2006
    Location
    Toronto, Ontario
    Posts
    2,270
    Rep Power
    9
  5. Nick Presta is on a distinguished road
  6. Re: Uptime command

    EDIT: To actually answer your question, the "uptime command" is a *NIX command that is used to display uptime, server load and users. You can use exec() in PHP to execute this.


    You can use these two "scripts" I wrote for my site.

    PHP Code:
      $reguptime trim(exec("uptime"));
      if(
    $reguptime) {
        if(
    preg_match("/, *(\d) (users?), .*: (.*), (.*), (.*)/"$reguptime$uptime)) {
          
    $loadnow $uptime[3]; // Server load right now
          
    $load15 $uptime[4]; // Server Load 15 minutes ago
        
    } else {
          
    $loadnow 'Unavailable';
          
    $load15 'Unavailable';
        }
      }

    echo 
    $loadnow;
    echo 
    $load15
    That will do server load.

    For a nice custom uptime, do this:

    PHP Code:
         // static uptime string
         
    function format_uptime($seconds) {

              
    $uptimeString '';
              
    $secs intval($seconds 60);
              
    $mins intval($seconds 60 60);
              
    $hours intval($seconds 3600 24);
              
    $days intval($seconds 86400);

              if (
    $days 0) {
                   
    $uptimeString .= $days;
                   
    $uptimeString .= (($days == 1) ? " day" " days");
              }
              if (
    $hours 0) {
                   
    $uptimeString .= (($days 0) ? ", " "") . $hours;
                   
    $uptimeString .= (($hours == 1) ? " hour" " hours");
              }
              if (
    $mins 0) {
                   
    $uptimeString .= (($days || $hours 0) ? ", " "") . $mins;
                   
    $uptimeString .= (($mins == 1) ? " minute" " minutes");
              }
              if (
    $secs 0) {
                   
    $uptimeString .= (($days || $hours || $mins 0) ? ", " "") . $secs;
                   
    $uptimeString .= (($secs == 1) ? " second" " seconds");
              }
              return 
    $uptimeString;
         }

         
    // read in the uptime (using exec)
         
    $uptime exec("cat /proc/uptime");
         
    $uptime split(" ",$uptime);
         
    $uptimeSecs $uptime[0];

         
    // get the static uptime
         
    $staticUptime 'Server Uptime: ' format_uptime($uptimeSecs); 
    Personally, I use: http://siteuptime.com/ with the script: http://nickpresta.ath.cx/sources4u/site_uptime.phps that I wrote.
    Last edited by Nick Presta; 10-31-2006 at 09:22 PM.
    Reply With Quote Reply With Quote
  7. #3
    iMatt's Avatar
    Join Date
    Jun 2005
    Location
    New Jersey
    Age
    21
    Posts
    853
    Rep Power
    10
  8. iMatt is just really nice iMatt is just really nice iMatt is just really nice iMatt is just really nice
  9. Re: Uptime command

    I tried the first one within another script I have for the actual time (I'm mainly looking for server load, etc. right now)
    and got unavailable, unavailable for both. Does it have to be in its own file?
    Reply With Quote Reply With Quote
  10. #4
    Helloadam's Avatar
    Join Date
    Aug 2004
    Location
    Los Angeles, California, USA
    Posts
    1,123
    Rep Power
    9
  11. Helloadam is on a distinguished road
  12. Re: Uptime command

    Hey,

    One thing that you might cause you to get that is if

    shell_exec

    is not set in your PHP.ini file or if you don't have access to that...

    You can also check out

    PHP Code:
    <?php
    $reguptime 
    trim(exec("uptime"));
    if (
    $reguptime) {
    if (
    preg_match("/, *(\d) (users?), .*: (.*), (.*), (.*)/"$reguptime$uptime)) {
    $users[0] = $uptime[1];
    $users[1] = $uptime[2];
    $loadnow $uptime[3];
    $load15 $uptime[4];
    $load30 $uptime[5];
    } else {
    $users[0] = "Unavailable";
    $users[1] = "--";
    $loadnow "Unavailable";
    $load15 "--";
    $load30 "--";
    }
    $uptime shell_exec("cut -d. -f1 /proc/uptime");
    $days floor($uptime/60/60/24);
    $hours str_pad($uptime/60/60%24,2,"0",STR_PAD_LEFT);
    $mins str_pad($uptime/60%60,2,"0",STR_PAD_LEFT);  
    $secs str_pad($uptime%60,2,"0",STR_PAD_LEFT);
    }

    echo 
    "$loadnow (Load like right right right now) - $load15 (load like 15 minutes ago) and $load30 (load like 30 min ago)";
    echo 
    "<br /><br />";
    echo 
    "Server up for $days Days and $hours hours $mins minutes and $secs";

    ?>
    Which should work, shows server load and uptime...

    From,
    Adam


    EastSolid.com Webhosting since 2004!
    Shared, Reseller, VPS and Dedicated Servers! + PHP5 and Ruby on Rails!
    FREE End User Support for Resellers!
    Reply With Quote Reply With Quote
  13. #5
    iMatt's Avatar
    Join Date
    Jun 2005
    Location
    New Jersey
    Age
    21
    Posts
    853
    Rep Power
    10
  14. iMatt is just really nice iMatt is just really nice iMatt is just really nice iMatt is just really nice
  15. Re: Uptime command

    The second part works but I'm still getting Unavailable for the first part.

    And I have no clue what you mean about shell_exec in PHP.ini.
    Reply With Quote Reply With Quote
  16. #6
    Helloadam's Avatar
    Join Date
    Aug 2004
    Location
    Los Angeles, California, USA
    Posts
    1,123
    Rep Power
    9
  17. Helloadam is on a distinguished road
  18. Re: Uptime command

    Hey,

    PHP funtions exec and shell_exec need to be enabled in your php.ini file. I think you have to enter it in disable_functions or something like that....

    You also need safe mod to be off...

    From,
    Adam


    EastSolid.com Webhosting since 2004!
    Shared, Reseller, VPS and Dedicated Servers! + PHP5 and Ruby on Rails!
    FREE End User Support for Resellers!
    Reply With Quote Reply With Quote
  19. #7
    iMatt's Avatar
    Join Date
    Jun 2005
    Location
    New Jersey
    Age
    21
    Posts
    853
    Rep Power
    10
  20. iMatt is just really nice iMatt is just really nice iMatt is just really nice iMatt is just really nice
  21. Re: Uptime command

    Quote Helloadam originally posted: View Post
    Hey,

    PHP funtions exec and shell_exec need to be enabled in your php.ini file. I think you have to enter it in disable_functions or something like that....

    You also need safe mod to be off...

    From,
    Adam
    :blushing:
    And where is php.ini? It doesn't show up when I just do domain.com/php.ini
    Reply With Quote Reply With Quote
  22. #8
    Helloadam's Avatar
    Join Date
    Aug 2004
    Location
    Los Angeles, California, USA
    Posts
    1,123
    Rep Power
    9
  23. Helloadam is on a distinguished road
  24. Re: Uptime command

    Hey,

    PHP.ini is the like the core settings file that PHP uses. I'm guessing you are running this script off of a shared hosting environment? If so you are going to need to contact your webhost about this (some of them turn it off for security purpose even tho its not). This requres root access to enable or change anything in the PHP.ini file.

    I'm not sure if you can put a .htaccess file in your public_html (guessing your hosts uses cPanel) folder so you have this option enabled...

    From,
    Adam


    EastSolid.com Webhosting since 2004!
    Shared, Reseller, VPS and Dedicated Servers! + PHP5 and Ruby on Rails!
    FREE End User Support for Resellers!
    Reply With Quote Reply With Quote
  25. #9
    iMatt's Avatar
    Join Date
    Jun 2005
    Location
    New Jersey
    Age
    21
    Posts
    853
    Rep Power
    10
  26. iMatt is just really nice iMatt is just really nice iMatt is just really nice iMatt is just really nice
  27. Re: Uptime command

    Nope, my host doesn't use cPanel (DreamHost).
    I'll just live with the time for now til I find a better option, thanks though.
    Reply With Quote Reply With Quote
  28. #10
    Helloadam's Avatar
    Join Date
    Aug 2004
    Location
    Los Angeles, California, USA
    Posts
    1,123
    Rep Power
    9
  29. Helloadam is on a distinguished road
  30. Re: Uptime command

    Hey,

    Well if the server uptime works then the other ones should work as well as they use the same command.

    Which script are you using? Nicks or mine?

    From,
    Adam


    EastSolid.com Webhosting since 2004!
    Shared, Reseller, VPS and Dedicated Servers! + PHP5 and Ruby on Rails!
    FREE End User Support for Resellers!
    Reply With Quote Reply With Quote

Similar Threads

  1. MySQL command works in phpMyAdmin but not in a PHP script
    By callumjones in forum PHP Scripting
    Replies: 5
    Last Post: 09-05-2005, 09:48 AM
  2. Uptime League
    By callumjones in forum Suggestions
    Replies: 42
    Last Post: 09-01-2005, 09:30 AM
  3. Creating a uptime GD image for Windows.
    By callumjones in forum PHP Articles
    Replies: 9
    Last Post: 06-18-2005, 08:42 PM
  4. How to freeze up your network using net send command
    By Helloadam in forum Articles, Tutorials, and Guides
    Replies: 12
    Last Post: 06-15-2005, 09:25 PM