hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Programming Discussion : uptime script
Reply

Forum Jump

uptime script

Reply Post New Thread In Programming Discussion Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 09-01-2002, 01:20 AM
mattschinkel mattschinkel is offline
Web Hosting Guru
 
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.



Sponsored Links
  #2  
Old 09-01-2002, 01:25 AM
The Prohacker The Prohacker is offline
iNET Interactive
 
Join Date: May 2001
Location: Dayton, Ohio
Posts: 4,894
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...

__________________
-Mat

  #3  
Old 09-01-2002, 02:32 AM
mattschinkel mattschinkel is offline
Web Hosting Guru
 
Join Date: Aug 2002
Location: Toronto, Canada
Posts: 286
Thanx... workes great!

__________________
www.prolinker.com - free automatic linking to your website

Sponsored Links
  #4  
Old 09-02-2002, 08:36 AM
dreamrae.com dreamrae.com is offline
Web Hosting Master
 
Join Date: Aug 2002
Location: Baltimore, Maryland
Posts: 580
cant you just do: echo shell("uptime"); on a linux box?

  #5  
Old 09-02-2002, 12:44 PM
mind21_98 mind21_98 is offline
Web Hosting Master
 
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  
Old 09-02-2002, 02:46 PM
The Prohacker The Prohacker is offline
iNET Interactive
 
Join Date: May 2001
Location: Dayton, Ohio
Posts: 4,894
Quote:
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  
Old 09-02-2002, 04:38 PM
OwenCK OwenCK is offline
Newbie
 
Join Date: Mar 2002
Posts: 10
Couldnt you also just use backquotes for shell commands? such as

<? echo `uptime` ?> ?

  #8  
Old 09-02-2002, 04:49 PM
The Prohacker The Prohacker is offline
iNET Interactive
 
Join Date: May 2001
Location: Dayton, Ohio
Posts: 4,894
Quote:
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  
Old 09-02-2002, 06:09 PM
RackNine RackNine is offline
Web Hosting Master
 
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  
Old 04-07-2004, 12:55 PM
XtReMaTriX XtReMaTriX is offline
New Member
 
Join Date: Apr 2004
Posts: 1
...2 Years Later (I am a little slow )

Quote:
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  
Old 04-07-2004, 01:26 PM
mind21_98 mind21_98 is offline
Web Hosting Master
 
Join Date: Dec 2000
Location: San Diego, CA
Posts: 1,571
Quote:
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  
Old 04-07-2004, 04:52 PM
ub3r ub3r is offline
Disabled
 
Join Date: Dec 2002
Location: chica go go
Posts: 11,858
PHP Code:
<?PHP system('uptime'); ?>

Reply

Related posts from TheWhir.com
Title Type Date Posted
Uptime Institute Symposium 2014 Web Hosting Events 2014-04-09 11:24:20
Service Providers Push Updates to Fight OpenSSL Heartbleed Bug Web Hosting News 2014-04-08 16:58:05
Web Hosting Sales and Promos Roundup - December 6, 2013 Web Hosting News 2013-12-06 15:09:54
Third Party Data Center Provider Budgets See Growth: Uptime Institute Survey Web Hosting News 2013-08-08 12:31:45
OpenStack and the AWS API Debate Web Hosting News 2013-07-30 14:21:54


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?