By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,201 Members | 937 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,201 IT Pros & Developers. It's quick & easy.

Server Reboot

P: n/a
Is there a way (through a variable) to show the date of the last server
reboot i.e. when PHP last started running? As I am trying to show the
server uptime, which of course only works from the last reboot,
therefore my %ages are very low.

Cheers,
Ben
Jul 17 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Ben Allen wrote:
Is there a way (through a variable) to show the date of the last server
reboot i.e. when PHP last started running? As I am trying to show the
server uptime, which of course only works from the last reboot,
therefore my %ages are very low.

You could use /proc/uptime to retrieve it.
For a php example, check this out:

http://www.xenocafe.com/tutorials/ph...time/index.php

--
http://www.phpforums.nl
Jul 17 '05 #2

P: n/a
Peter van Schie wrote:
You could use /proc/uptime to retrieve it.
For a php example, check this out:

http://www.xenocafe.com/tutorials/ph...time/index.php


I just noticed that that tutorial has a lot of unnecessary mumbo jumbo
in it.
Here's a simpler code snippet of how to use /proc/uptime:

<?php
$uptime = shell_exec("cut -d. -f1 /proc/uptime");
$days = floor($uptime / 60 / 60 / 24);
$hours = $uptime / 60 / 60 % 24;
$mins = $uptime / 60 % 60;
$secs = $uptime % 60;
echo 'Uptime is: ' . $days . ' days ' . $hours . ' hours ' . $mins
minutes . ' and ' . $secs . ' seconds';
?>

--
http://www.phpforums.nl
Jul 17 '05 #3

P: n/a
Peter van Schie wrote:
Peter van Schie wrote:
You could use /proc/uptime to retrieve it.
For a php example, check this out:

http://www.xenocafe.com/tutorials/ph...time/index.php


I just noticed that that tutorial has a lot of unnecessary mumbo jumbo
in it.
Here's a simpler code snippet of how to use /proc/uptime:

<?php
$uptime = shell_exec("cut -d. -f1 /proc/uptime");
$days = floor($uptime / 60 / 60 / 24);
$hours = $uptime / 60 / 60 % 24;
$mins = $uptime / 60 % 60;
$secs = $uptime % 60;
echo 'Uptime is: ' . $days . ' days ' . $hours . ' hours ' . $mins
minutes . ' and ' . $secs . ' seconds';
?>

Thanks, that code (with a few tweaks involving the minutes text-oops)
gives me:
Uptime is: 5 days 21 hours 34 minutes and 44 seconds

which is the same as what my other script was giving me and is the
uptime since the last reboot. Obviously this posses problems when trying
to calculate a percentage.

Thanks
Ben
Jul 17 '05 #4

P: n/a
Ben Allen wrote:
Thanks, that code (with a few tweaks involving the minutes text-oops)
gives me:
Uptime is: 5 days 21 hours 34 minutes and 44 seconds

which is the same as what my other script was giving me and is the
uptime since the last reboot. Obviously this posses problems when trying
to calculate a percentage.


Sorry I must have misinterpreted your question then. What percentage are
you trying to calculate?
--
http://www.phpforums.nl
Jul 17 '05 #5

P: n/a
>> <?php
$uptime = shell_exec("cut -d. -f1 /proc/uptime");
$days = floor($uptime / 60 / 60 / 24);
$hours = $uptime / 60 / 60 % 24;
$mins = $uptime / 60 % 60;
$secs = $uptime % 60;
echo 'Uptime is: ' . $days . ' days ' . $hours . ' hours ' . $mins
minutes . ' and ' . $secs . ' seconds';
?>
Thanks, that code (with a few tweaks involving the minutes text-oops)
gives me:
Uptime is: 5 days 21 hours 34 minutes and 44 seconds

which is the same as what my other script was giving me and is the
uptime since the last reboot.


If you want the uptime in just seconds, $uptime in the above
script gives you that.
Obviously this posses problems when trying
to calculate a percentage.


What percentage? What problems? Since you haven't said what you
want, nothing is obvious.

Gordon L. Burditt
Jul 17 '05 #6

P: n/a
Gordon Burditt wrote:
<?php
$uptime = shell_exec("cut -d. -f1 /proc/uptime");
$days = floor($uptime / 60 / 60 / 24);
$hours = $uptime / 60 / 60 % 24;
$mins = $uptime / 60 % 60;
$secs = $uptime % 60;
echo 'Uptime is: ' . $days . ' days ' . $hours . ' hours ' . $mins
minutes . ' and ' . $secs . ' seconds';
?>

Thanks, that code (with a few tweaks involving the minutes text-oops)
gives me:
Uptime is: 5 days 21 hours 34 minutes and 44 seconds

which is the same as what my other script was giving me and is the
uptime since the last reboot.

If you want the uptime in just seconds, $uptime in the above
script gives you that.

Obviously this posses problems when trying
to calculate a percentage.

What percentage? What problems? Since you haven't said what you
want, nothing is obvious.

Gordon L. Burditt


Sorry, basically, I wanted to calculate uptime as a percentage
(preferably over the year), however, I then found the $uptime variable
only counted since the last reboot, therefore I wanted to show when the
server was last rebooted and also divide uptime by the server reboot
time. This is the code i was using, please note, i have added variables
into the 'start date' so I can set the reboot date manually. I wondered
if there was a way to do this manually.

<?php
$uptime = @exec('uptime');
preg_match("/averages?:
([0-9\.]+),[\s]+([0-9\.]+),[\s]+([0-9\.]+)/",$uptime,$avgs);
$uptime = explode(' up ', $uptime);
$uptime = explode(',', $uptime[1]);
$uptime = $uptime[0].', '.$uptime[1];
$sday = 12;
$smonth = 07;
$syear = 2005;
$start=mktime(0, 0, 0, $smonth, $sday, $syear, 0);
$end=mktime(0, 0, 0, date("m"), date("d"), date("Y"), 0);
$diff=$end-$start;
$days=$diff/86400;
$percentage= round(($uptime/$days) * 100, 2);
#$load=$avgs[1].",".$avgs[2].",".$avgs[3]."";
$page='<style type="text/css">
<!--
..style1 {
color: #000000;
font-weight: bold;
}
..style2 {font-size: 12px}
-->
</style>
<body>
<table border="0" align="center" cellpadding="0" cellspacing="0"
style="border: 1 solid #000000">
<tr>
<td width="150" bgcolor="#F4F4F4" style="border: 1 solid
#000000"><div align="center"><span class="style1"><b>U</b>ptime:
<strong>'.$uptime.'</strong></span><br>
</div></td>
</tr>
<tr>
<td width="150" bgcolor="#F4F4F4" style="border: 1 solid
#000000"><div align="center">
<p><span
class="style1"><strong>'.$percentage.'%</strong></span><strong><br>
</strong>(since last reboot)</p>
</div></td>
</tr>
<tr>
<td width="150" bgcolor="#F4F4F4" style="border: 1 solid
#000000"><div align="center"><span class="style2">Server Last Rebooted
on 12/07/2005 </span></div></td>
</tr>
</table>
</body>';
echo $page;
?>
Jul 18 '05 #7

P: n/a
Ben Allen wrote:
Sorry, basically, I wanted to calculate uptime as a percentage
(preferably over the year), however, I then found the $uptime variable
only counted since the last reboot, therefore I wanted to show when the
server was last rebooted and also divide uptime by the server reboot
time. This is the code i was using, please note, i have added variables
into the 'start date' so I can set the reboot date manually. I wondered
if there was a way to do this manually.


I don't think that's possible, because to calculate that you'd need to
know for how long the server has been down before the last reboot.
I can't think of any file/device to retrieve that on a Linux box, I
might be wrong though.
--
http://www.phpforums.nl
Jul 18 '05 #8

P: n/a
Ben,

Won't work.

All you can determine is how long the machine has been up since the last reboot.
You can't tell how long it was down.

I see two ways around it. Either append the current date and time to a file
every minute (depending on the accuracy you want) and parse the file to find the
missing times, or monitor the site from a remote system.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 18 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.