mattschinkel
09-01-2002, 01:20 AM
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.
I am on a linux server.
![]() | View Full Version : uptime script mattschinkel 09-01-2002, 01:20 AM 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. The Prohacker 09-01-2002, 01:25 AM <? $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... mattschinkel 09-01-2002, 02:32 AM Thanx... workes great! dreamrae.com 09-02-2002, 08:36 AM cant you just do: echo shell("uptime"); on a linux box? mind21_98 09-02-2002, 12:44 PM /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(); ?> The Prohacker 09-02-2002, 02:46 PM 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... 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... OwenCK 09-02-2002, 04:38 PM Couldnt you also just use backquotes for shell commands? such as <? echo `uptime` ?> ? The Prohacker 09-02-2002, 04:49 PM 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.... RackNine 09-02-2002, 06:09 PM 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 XtReMaTriX 04-07-2004, 12:55 PM ...2 Years Later (I am a little slow :stickout) 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(); ?> mind21_98 04-07-2004, 01:26 PM Originally posted by XtReMaTriX ...2 Years Later (I am a little slow :stickout) 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! ub3r 04-07-2004, 04:52 PM <?PHP system('uptime'); ?> |