Results 1 to 12 of 12

Thread: uptime script

  1. #1
    Join Date
    Aug 2002
    Location
    Toronto, Canada
    Posts
    286

    uptime script

    I would like to be able to show users how long my server has been up. Anyone have a php script for this?

    I am on a linux server.

  2. #2
    Join Date
    May 2001
    Location
    Dayton, Ohio
    Posts
    4,977
    PHP Code:
    <?
    $text
    ['minutes'] = "minutes";
    $text['hours'] = "hours";
    $text['days'] = "days";

        function 
    uptime ()
        {
            global 
    $text;
            
    $fd fopen('/proc/uptime''r');
            
    $ar_buf split(' 'fgets($fd4096));
            
    fclose($fd);

            
    $sys_ticks trim($ar_buf[0]);

            
    $min   $sys_ticks 60;
            
    $hours $min 60;
            
    $days  floor($hours 24);
            
    $hours floor($hours - ($days 24));
            
    $min   floor($min - ($days 60 24) - ($hours 60));

            if (
    $days != 0) {
                
    $result "$days ".$text['days']." ";
            }

            if (
    $hours != 0) {
                
    $result .= "$hours ".$text['hours']." ";
            }  
            
    $result .= "$min ".$text['minutes'];

            return 
    $result;     
        }


    $ack uptime()



    echo 
    $ack
    ?>
    The function is basicly taken directly from phpSysIfno...

  3. #3
    Join Date
    Aug 2002
    Location
    Toronto, Canada
    Posts
    286
    Thanx... workes great!
    www.prolinker.com - free automatic linking to your website

  4. #4
    Join Date
    Aug 2002
    Location
    Baltimore, Maryland
    Posts
    580
    cant you just do: echo shell("uptime"); on a linux box?

  5. #5
    Join Date
    Dec 2000
    Location
    San Diego, CA
    Posts
    1,571
    /proc/uptime doesn't exist on FreeBSD boxes:

    mooneer@capricorn:~$ cat /proc/uptime
    cat: /proc/uptime: No such file or directory
    mooneer@capricorn:~$

    You can try this instead if you want something that works in all Unixes:

    <?php
    function uptime() {
    $uptime = `uptime`;
    preg_match("/up (.+), (\\d+) users/", $uptime, $matches);
    return $matches[1];
    }

    echo uptime();
    ?>
    -Mooneer
    Thoughtbug Software: Hosting shouldn't require any thought.
    Legitimate host? Support the Code of Ethical Conduct

  6. #6
    Join Date
    May 2001
    Location
    Dayton, Ohio
    Posts
    4,977
    Originally posted by mind21_98
    /proc/uptime doesn't exist on FreeBSD boxes:

    mooneer@capricorn:~$ cat /proc/uptime
    cat: /proc/uptime: No such file or directory
    mooneer@capricorn:~$

    You can try this instead if you want something that works in all Unixes:

    <?php
    function uptime() {
    $uptime = `uptime`;
    preg_match("/up (.+), (\\d+) users/", $uptime, $matches);
    return $matches[1];
    }

    echo uptime();
    ?>
    He didn't ask for FreeBSD, otherwise I would have gave him the code used in phpSysInfo for BSDs...


    PHP Code:
        function uptime () {
            
    $sys_ticks $this->get_sys_ticks();

            
    $min   $sys_ticks 60;
            
    $hours $min 60;
            
    $days  floor($hours 24);
            
    $hours floor($hours - ($days 24));
            
    $min   floor($min - ($days 60 24) - ($hours 60));

            if ( 
    $days != ) {
                
    $result "$days days, ";
            }

            if ( 
    $hours != ) {
                
    $result .= "$hours hours, ";
            }
            
    $result .= "$min minutes";

            return 
    $result;
        }


        function 
    get_sys_ticks () {
            
    $s explode(' '$this->grab_key('kern.boottime'));
            
    $a ereg_replace('{ '''$s[3]);
            
    $sys_ticks time() - $a;
            return 
    $sys_ticks;
        }

    $uptime uptime();

    echo 
    $uptime;
    ?> 
    Calling a program isn't always a good idea, this method uses the kernel keys to get information instead of using something like /proc/uptime...

  7. #7
    Couldnt you also just use backquotes for shell commands? such as

    <? echo `uptime` ?> ?

  8. #8
    Join Date
    May 2001
    Location
    Dayton, Ohio
    Posts
    4,977
    Originally posted by OwenCK
    Couldnt you also just use backquotes for shell commands? such as

    <? echo `uptime` ?> ?

    Yes but its hard to control the way it looks with just that...

    Sometimes 4:54pm up 47 days, 20:42, 1 user, load average: 0.00, 0.00, 0.00

    Isn't what you want on the page....

  9. #9
    Join Date
    May 2002
    Location
    Edmonton, Canada
    Posts
    978
    Simple little implementation of the 'w' command:

    http://support.racknine.com/uptime.php

    Monitors 5 random servers we have at RackShack. We have a hardware device w/ MRTG for our local stuff, this is a neat idea to track and eventually graph processor utilization I toyed with awhile ago.

    Sincerely,

    -Matt
    Matt Meier, RackNine Inc.
    email: mmeier@racknine.com
    web: http://www.racknine.com

  10. #10
    ...2 Years Later (I am a little slow )

    Originally posted by mind21_98
    /proc/uptime doesn't exist on FreeBSD boxes:

    mooneer@capricorn:~$ cat /proc/uptime
    cat: /proc/uptime: No such file or directory
    mooneer@capricorn:~$

    You can try this instead if you want something that works in all Unixes:

    <?php
    function uptime() {
    $uptime = `uptime`;
    preg_match("/up (.+), (\\d+) users/", $uptime, $matches);
    return $matches[1];
    }

    echo uptime();
    ?>
    Actually, I found that if you have one person logged into SSH the script will not run. I had to remove the 's' from 'users/':

    <?php
    function uptime() {
    $uptime = `uptime`;
    preg_match("/up (.+), (\\d+) user/", $uptime, $matches);
    return $matches[1];
    }

    echo uptime();
    ?>
    Last edited by XtReMaTriX; 04-07-2004 at 01:02 PM.

  11. #11
    Join Date
    Dec 2000
    Location
    San Diego, CA
    Posts
    1,571
    Originally posted by XtReMaTriX
    ...2 Years Later (I am a little slow )



    Actually, I found that if you have one person logged into SSH the script will not run. I had to remove the 's' from 'users/':

    <?php
    function uptime() {
    $uptime = `uptime`;
    preg_match("/up (.+), (\\d+) user/", $uptime, $matches);
    return $matches[1];
    }

    echo uptime();
    ?>
    Wow. I didn't notice. Thanks!
    -Mooneer
    Thoughtbug Software: Hosting shouldn't require any thought.
    Legitimate host? Support the Code of Ethical Conduct

  12. #12
    Join Date
    Dec 2002
    Location
    chica go go
    Posts
    11,876
    PHP Code:
    <?PHP system('uptime'); ?>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •