Sorry, I'm getting a little long winded here, but it has been a long day..
So far I've seen these outputs for uptime in NetBSD 1.6.2 (The version I'm using).
9:55AM up 1 min, 1 user, load averages: 0.11, 0.12, 0.14
10:55AM up 1 hr, 1 user, load averages: 0.11, 0.12, 0.14
12:00PM up 6 days, 14:42, 1 user, load averages: 0.25, 0.17, 0.10
10:26AM up 32 mins, 1 user, load averages: 0.11, 0.12, 0.14
10:56AM up 1:03, 1 user, load averages: 0.25, 0.24, 0.18
10:56AM up 6 days, 32 mins, 1 user, load averages: 0.25, 0.24, 0.18
8:53AM up 23 hrs, 1 user, load averages: 0.13, 0.13, 0.09
9:54AM up 1 day, 1 user, load averages: 0.34, 0.29, 0.16
9:55AM up 1 day, 1 min, 1 user, load averages: 0.44, 0.31, 0.17
10:55AM up 1 day, 1 hr, 1 user, load averages: 0.44, 0.31, 0.17
I wanted to parse uptime's output for days, hours and minutes. Parsing uptime is a nightmare because the fields constantly change. I did write a script to parse all the above examples, but it just didn't look good with all those case and if statements. I decided to see if I could find out where uptime gets it's data. I never found it, but I found out that "sysctl kern.boottime" is the time your system started and "sysctl -n kern.boottime" gives you the time in seconds. So, now I know when my system started but not how much time has passed since it started. I did a lot of googling and really couldn't find the answer. I know it is in the /proc directory. I found it is in field 8 of /proc/curproc/status. This is a symbolic link to the last process run. For example if you do a "cat /proc/curproc/status" you will be looking at the process for cat command you just typed. Anyway, field 8 contains the seconds since your system started and the last process was run. Don't worry about this being old because the script to get the Days, Hours and Minutes will start several processes.
So, finally I know the systems boot time and I know the current time. Now it's just a matter of subtracting the boot time from the current time. Breaking it into days, hours and minutes (even seconds if I wanted to). Here's the part of my script that does just that:
#!/bin/csh
#
set curtime=` cat /proc/curproc/status | awk '{ print $8 }' | awk -F, '{ print $1 }' `
set bootime=` sysctl -n kern.boottime `
@ systime = $curtime - $bootime
@ minutes = ($systime / 60) % 60
@ hours = ($systime / 3600) % 24
@ days = $systime / 86400
echo "This System has been up " $days "day(s), " $hours " hour(s), and " $minutes " minute(s)."
Seems like a big deal for nothing, I know, but I want to output this as a HTML and run it from a crontab and update the webserver as a human readable output instead of the uptime output. That, and I didn't have anything better to do. What next? I'm going to run it from a CGI and make it a button that prints the updated time everytime the button is pressed.
I also wrote a script to update my dynamic ip (zoneedit). I'm having fun...
So far I've seen these outputs for uptime in NetBSD 1.6.2 (The version I'm using).
9:55AM up 1 min, 1 user, load averages: 0.11, 0.12, 0.14
10:55AM up 1 hr, 1 user, load averages: 0.11, 0.12, 0.14
12:00PM up 6 days, 14:42, 1 user, load averages: 0.25, 0.17, 0.10
10:26AM up 32 mins, 1 user, load averages: 0.11, 0.12, 0.14
10:56AM up 1:03, 1 user, load averages: 0.25, 0.24, 0.18
10:56AM up 6 days, 32 mins, 1 user, load averages: 0.25, 0.24, 0.18
8:53AM up 23 hrs, 1 user, load averages: 0.13, 0.13, 0.09
9:54AM up 1 day, 1 user, load averages: 0.34, 0.29, 0.16
9:55AM up 1 day, 1 min, 1 user, load averages: 0.44, 0.31, 0.17
10:55AM up 1 day, 1 hr, 1 user, load averages: 0.44, 0.31, 0.17
I wanted to parse uptime's output for days, hours and minutes. Parsing uptime is a nightmare because the fields constantly change. I did write a script to parse all the above examples, but it just didn't look good with all those case and if statements. I decided to see if I could find out where uptime gets it's data. I never found it, but I found out that "sysctl kern.boottime" is the time your system started and "sysctl -n kern.boottime" gives you the time in seconds. So, now I know when my system started but not how much time has passed since it started. I did a lot of googling and really couldn't find the answer. I know it is in the /proc directory. I found it is in field 8 of /proc/curproc/status. This is a symbolic link to the last process run. For example if you do a "cat /proc/curproc/status" you will be looking at the process for cat command you just typed. Anyway, field 8 contains the seconds since your system started and the last process was run. Don't worry about this being old because the script to get the Days, Hours and Minutes will start several processes.
So, finally I know the systems boot time and I know the current time. Now it's just a matter of subtracting the boot time from the current time. Breaking it into days, hours and minutes (even seconds if I wanted to). Here's the part of my script that does just that:
#!/bin/csh
#
set curtime=` cat /proc/curproc/status | awk '{ print $8 }' | awk -F, '{ print $1 }' `
set bootime=` sysctl -n kern.boottime `
@ systime = $curtime - $bootime
@ minutes = ($systime / 60) % 60
@ hours = ($systime / 3600) % 24
@ days = $systime / 86400
echo "This System has been up " $days "day(s), " $hours " hour(s), and " $minutes " minute(s)."
Seems like a big deal for nothing, I know, but I want to output this as a HTML and run it from a crontab and update the webserver as a human readable output instead of the uptime output. That, and I didn't have anything better to do. What next? I'm going to run it from a CGI and make it a button that prints the updated time everytime the button is pressed.
I also wrote a script to update my dynamic ip (zoneedit). I'm having fun...