Jump to content
EduGeek EdSec 2026 is Go! 27th Oct in Derby! Join us for a day of EdTech security focused talks, networking, and an evening social ×

Recommended Posts

Posted

I know uptime will give the user the uptime in a format of:

 

[CURRENT TIME][uPTIME][uSERS ONLINE][LOAD AVERAGE]

 

But how do I get it so it only returns the UPTIME part?

Posted

Hacking up the line with the 'cut' command does the job.

 

uptime | cut -d' '  -f3-6

 

the other option is to take the info directly from /proc/uptime.

 

Edit:Bah too slow!

Posted
I just tried cat /proc/uptime and got:

 

96789.49 95483.24

 

:confused:

 

The first is the uptime in seconds of the system. The second is the total idle time of the system since boot.

 

Consider the following C++ program.

 

#include 
#include 
#include 
#include 
#include 

using namespace std;

struct UptimeStats {

int nTotalSeconds;

int nSeconds;
int nMinutes;
int nHours;
int nDays;
// years typically aren't presented.  "400 days" instead of "1 year, 35 days"

};

int main ( )
{

ifstream uptime ( "/proc/uptime" );

double nUptimeSecondsDouble;

// Have to read it in as a double, or the next read is messed up
uptime >> nUptimeSecondsDouble;
int nUptimeSeconds = (int)nUptimeSecondsDouble;

printf ( "%d\n", nUptimeSeconds );

// create the UptimeStats object
UptimeStats oUptimeStats;

oUptimeStats.nTotalSeconds = nUptimeSeconds;

oUptimeStats.nSeconds = nUptimeSeconds % 60;
oUptimeStats.nMinutes = nUptimeSeconds / 60 % 60;
oUptimeStats.nHours = nUptimeSeconds / 3600 % 24;
oUptimeStats.nDays = nUptimeSeconds / 86400;

printf ( "%d days %d hours %d minutes %d seconds\n",
		 oUptimeStats.nDays,
		 oUptimeStats.nHours,
		 oUptimeStats.nMinutes,
		 oUptimeStats.nSeconds );

///////////////////////////////////////////////////////////////////////////
// Same thing again to handle idle seconds
///////////////////////////////////////////////////////////////////////////

int nIdleSecondsDouble;

uptime >> nIdleSecondsDouble;

int nIdleSeconds = (int) nIdleSecondsDouble;

printf ( "%d\n", nIdleSeconds );

// create the UptimeStats object
UptimeStats oIdleStats;

oIdleStats.nTotalSeconds = nIdleSeconds;

oIdleStats.nSeconds = nIdleSeconds % 60;
oIdleStats.nMinutes = nIdleSeconds / 60 % 60;
oIdleStats.nHours = nIdleSeconds / 3600 % 24;
oIdleStats.nDays = nIdleSeconds / 86400;

printf ( "%d days %d hours %d minutes %d seconds\n",
		 oIdleStats.nDays,
		 oIdleStats.nHours,
		 oIdleStats.nMinutes,
		 oIdleStats.nSeconds );


}

  • 2 weeks later...
Posted

Ok, I've just quickly jotted together this function:

 

function uptime(){
   //Get the raw uptime (in seconds) of the Linux system
   $seconds = shell_exec("cat /proc/uptime | cut -d'.' -f1-1");
   //Turn the number of seconds into days
   $seconds = (($seconds / 60) / 60) / 24;

   //Return the number of days to the system
   return $seconds;
}

 

This successfully returns the amount of uptime in days such as:

 

37.1237865

 

What would be the easiest way to make this a whole number rather than one with decimals?

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...