# uptime.csh
# Copyright (c) 2010-2012 by Hamilton Laboratories. All rights reserved.
# Uptime takes no arguments (and ignores any given) and prints a simple
# message telling the days, hours, minutes and seconds the system has been
# up. For example,
#
# 116 C% uptime
# 11 days, 19 hours, 41 minutes, 02 seconds
# This is calculated from the elapsed time for p0, the system idle
# process.
proc uptime( )
local elapsed, days, hours, min, sec
# This next line works like this to get hours, minutes and seconds
# as three separate words.
#
# 117 C% ps -s | g Idle
# p0 Idle 283:49:03 2221:19:27.00 ......
# 118 C% echo `ps -s | g Idle`:2
# 283:49:19
# 119 C% echo `ps -s | g Idle`:2:gs/:/ /
# 283 49 29
@ elapsed = `ps -s | grep Idle`:2:gs/:/ /
@ hours = elapsed[0]
@ min = elapsed[1]
@ sec = elapsed[2]
if ( hours >= 24 ) then
@ days = hours // 24
@ hours %= 24
echo $days days, $hours hours, $min minutes, $sec seconds
else
if ( hours >= 1 ) then
echo $hours hours, $min minutes, $sec seconds
else
if ( min >= 1 ) then
echo $min minutes, $sec seconds
else
echo $sec seconds
end
end
end
end
uptime
|