source code of /small-scripts/stackexchange/uptimerecord
Last modified | |
Lines | 48 |
Parent directory Download CGIread sitemap Main page
Quick links: (none)
#!/bin/sh
format_time ()
{
t=$1
days=$(($t / 86400))
t=$(($t - 86400*$days))
hours=$(($t / 3600))
t=$(($t - 3600*$hours))
minutes=$(($t / 60))
t=$(($t - 60*$minutes))
echo "$days days $hours hours $minutes minutes $t seconds"
}
main ()
{
record_file="$1"
# Old record
record="$(cat "$record_file")"
# When the system started.
boot="$(date -d "$(uptime -s)" '+%s')"
while true; do
# You could also calculate the uptime before the loop and just
# increment it, but that will rely on the script running 24/7.
now="$(date '+%s')"
current_uptime="$(($now - $boot))"
# Set and save the record when it gets beaten
if [ "$current_uptime" -gt "$record" ]; then
record="$current_uptime"
echo "$record" > "$record_file"
fi
# Status should be printed _after_ potentially having set
# a new record:
clear
echo "Current uptime: $(format_time "$current_uptime")"
echo "Record uptime: $(format_time "$record")"
# Don't waste system resources for silly things.
sleep 1
done
}
main ~/uptimerecord.data