How do I determine the uptime on a SunOS UNIX box in seconds only?
On Linux, I could simply cat /proc/uptime & take the first argument:
cat /proc/uptime | awk '{print $1}'
I'm trying to do the same on a SunOS UNIX box, but there is no /proc/uptime. There is an uptime command which presents the following output:
$ uptime
12:13pm up 227 day(s), 15:14, 1 user, load average: 0.05, 0.05, 0.05
I don't really want to have to write code to convert the date into seconds only & I'm sure someone must have had this requirement before but I have been unable to find anything on the internet.
Can anyone tell me how to get the uptime in just seconds?
TIA
First, open the terminal window and then type: uptime command – Tell how long the Linux system has been running. w command – Show who is logged on and what they are doing including the uptime of a Linux box. top command – Display Linux server processes and display system Uptime in Linux too.
/proc/uptime pseudo-file contains two numbers: The first number is how long the system has been up in seconds. The second number is how much of that time the machine has spent idle in seconds.
Solution: In order to get the linux uptime in seconds, Go to bash and type cat /proc/uptime . Parse the first number and convert it according to your requirement. The First number is the total number of seconds the system has been up. The Second number is how much of that time the machine has spent idle, in seconds.
If you don't mind compiling a small C program, you could use:
#include <stdio.h>
#include <string.h>
#include <utmpx.h>
int main()
{
int nBootTime = 0;
int nCurrentTime = time ( NULL );
struct utmpx * ent;
while ( ( ent = getutxent ( ) ) ) {
if ( !strcmp ( "system boot", ent->ut_line ) ) {
nBootTime = ent->ut_tv.tv_sec;
}
}
printf ( "System was booted %d seconds ago\n", nCurrentTime - nBootTime );
endutxent();
return 0;
}
Source: http://xaxxon.slackworks.com/rsapi/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With