Linux uptime
By: Daniel

If you want to show the uptime of your linux server on your website, you can do so very easily.

<?php
function linuxUptime() {
  $ut = strtok( exec( "cat /proc/uptime" ), "." );
  $days = sprintf( "%2d", ($ut/(3600*24)) );
  $hours = sprintf( "%2d", ( ($ut % (3600*24)) / 3600) );
  $min = sprintf( "%2d", ($ut % (3600*24) % 3600)/60  );
  $sec = sprintf( "%2d", ($ut % (3600*24) % 3600)%60  );
  return array( $days, $hours, $min, $sec );
}

$ut = linuxUptime();
// If you would like to show the seconds as well just add [ , $ut[3] seconds ] after minutes.
echo "Time since last reboot: $ut[0] days, $ut[1] hours, $ut[2] minutes";
?>

Just call the $ut = linuxUptime() function and then you can use the variables to show the time.

**NOTE: Your server must allow you to run the exec() php command. Some hosts disable the execution of this and several other php functions.

4 Votes | Average: 5 out of 54 Votes | Average: 5 out of 54 Votes | Average: 5 out of 54 Votes | Average: 5 out of 54 Votes | Average: 5 out of 5 (4 votes, average: 5 out of 5)
Loading ... Loading ...
del.icio.us:Linux uptime digg:Linux uptime spurl:Linux uptime wists:Linux uptime simpy:Linux uptime newsvine:Linux uptime blinklist:Linux uptime furl:Linux uptime reddit:Linux uptime fark:Linux uptime blogmarks:Linux uptime Y!:Linux uptime smarking:Linux uptime magnolia:Linux uptime segnalo:Linux uptime

4 Responses to “Linux uptime”

  1. xzibiz Says:

    How can i get this script to connect to many servers, and find and echo all the uptimes for all the servers ??

  2. WLScripting.com » Gather Linux Uptime (Multiple servers) Says:

    […] This code loops through the $sites array to determine which sites to gather information from. On every server you wish to check the uptime, create a file in the root of the domain called: linuxUptime.php. In this file you should include the code found in the Linux Uptime tutorial and only echo out the uptime. Anything that is output in the linuxUptime.php file will show up in the above output. […]

  3. Daniel Says:

    Here is a tutorial to gather uptime from multiple servers.

    http://www.wlscripting.com/tutorial/42

  4. s33n Says:

    how to make to show the max uptime of the server and from when the server runing?

Leave a Reply

eXTReMe Tracker
geovisitors