
09-01-2002, 01:20 AM
|
|
Web Hosting Guru
|
|
Join Date: Aug 2002
Location: Toronto, Canada
Posts: 286
|
|
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
|
|
iNET Interactive
|
|
Join Date: May 2001
Location: Dayton, Ohio
Posts: 4,870
|
|
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
?>
The function is basicly taken directly from phpSysIfno...
__________________
-Mat
|

09-01-2002, 02:32 AM
|
|
Web Hosting Guru
|
|
Join Date: Aug 2002
Location: Toronto, Canada
Posts: 286
|
|
__________________
www.prolinker.com - free automatic linking to your website
|

09-02-2002, 08:36 AM
|
|
Web 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
|
|
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();
?>
|

09-02-2002, 02:46 PM
|
|
iNET Interactive
|
|
Join Date: May 2001
Location: Dayton, Ohio
Posts: 4,870
|
|
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 != 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;
?>
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...
|

09-02-2002, 04:38 PM
|
|
Newbie
|
|
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
|
|
iNET Interactive
|
|
Join Date: May 2001
Location: Dayton, Ohio
Posts: 4,870
|
|
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....
|

09-02-2002, 06:09 PM
|
|
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
|

04-07-2004, 12:55 PM
|
|
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.
|

04-07-2004, 01:26 PM
|
|
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!
|

04-07-2004, 04:52 PM
|
|
Disabled
|
|
Join Date: Dec 2002
Location: chica go go
Posts: 11,858
|
|
PHP Code:
<?PHP system('uptime'); ?>
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
| 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
HTML code is Off
|
|
|
|
|
|
| Login: |
|
|
| Advertisement: |
|
|
| Web Hosting News: |
|
|
|