Results 1 to 12 of 12
Thread: uptime script
-
09-01-2002, 01:20 AM #1Web 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.
-
09-01-2002, 01:25 AM #2Web Hosting Master
- 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($fd, 4096));
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
?>
-
09-01-2002, 02:32 AM #3Web Hosting Guru
- Join Date
- Aug 2002
- Location
- Toronto, Canada
- Posts
- 286
Thanx... workes great!
www.prolinker.com - free automatic linking to your website
-
09-02-2002, 08:36 AM #4Web Hosting Master
- Join Date
- Aug 2002
- Location
- Baltimore, Maryland
- Posts
- 580
cant you just do: echo shell("uptime"); on a linux box?
-
09-02-2002, 12:44 PM #5Web 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
-
09-02-2002, 02:46 PM #6Web Hosting Master
- 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();
?>
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 != 0 ) {
$result = "$days days, ";
}
if ( $hours != 0 ) {
$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;
?>
-
09-02-2002, 04:38 PM #7Newbie
- Join Date
- Mar 2002
- Posts
- 10
Couldnt you also just use backquotes for shell commands? such as
<? echo `uptime` ?> ?
-
09-02-2002, 04:49 PM #8Web Hosting Master
- 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....
-
09-02-2002, 06:09 PM #9Web 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
-
04-07-2004, 12:55 PM #10New Member
- Join Date
- Apr 2004
- Posts
- 1
...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();
?>
<?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.
-
04-07-2004, 01:26 PM #11Web Hosting Master
- 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();
?>-Mooneer
Thoughtbug Software: Hosting shouldn't require any thought.
Legitimate host? Support the Code of Ethical Conduct
-
04-07-2004, 04:52 PM #12Disabled
- Join Date
- Dec 2002
- Location
- chica go go
- Posts
- 11,876
PHP Code:<?PHP system('uptime'); ?>