-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuptimelog.sh
executable file
·117 lines (85 loc) · 2.98 KB
/
uptimelog.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/bin/bash
#
# cron script to remember the best uptime on a computer
# Patrick CAO HUU THIEN <patrick.cao_huu_thien@upmc.fr>
#
readonly VERSION=2
# History
# * 15 May 2013 - 1
# - initial version
# * Dec 9 2013 - 2
# - add list all uptime recorded
function usage() {
cat <<EOT
Usage: $(basename $0) [ -q -l ]
crontab script to save best uptime in $HOME/.uptime-<date>
print new record in human readable format
options:
-q : no output
-l : list all uptime recorded
EOT
}
## Functions ##############################################
function do_debug () { [ $DEBUG ] && echo "[debug] $@" >&2 || false; }
#do_debug bla bla in debug
#do_debug mode debug || echo mode not debug
function do_crit () { cal=`caller 0`;echo "E: (line: $cal) $@" >&2; exit 1; }
#do_crit This is an critical error
function do_err () { echo "E: $@" >&2; exit 1; }
#do_err This is an error test
function do_err_usage () { echo "E: $@" >&2; usage; exit 1; }
#do_err_usage This is an error with usage
function do_warn () { echo "W: $@"; }
#do_warn This is an error test
function do_print () { [ -z $QUIET ] && echo "$@"; }
#do_print this is a test
function do_printf () { [ -z $QUIET ] && printf "$@"; }
#do_printf "%10s_%20s_%-10s_%s\n" this is a test
function do_verbose () { [ -n "$VERBOSE" ] && echo "$@"; }
function do_test () { [ -n "$TEST" ] && { [ -n "$*" ] && echo "[test] $@" || true; } || false; }
#do_test echo test || echo mode production
#do_test || echo mode production2
function do_trap_user() { echo "Interuption by user"; }
function do_trap_exit() { echo "exit prout"; }
## Arguments ##############################################
OPTIND=1
while getopts hnvVdql opt ; do
case "$opt" in
p) PROUT="$OPTARG";;
l) LISTING=1;;
h) usage; exit;;
v) VERBOSE=1;;
d) DEBUG=1;;
q) QUIET=1;;
n) TEST=1;;
V) echo "$(basename $0) - $VERSION"; exit;;
esac
done
shift $(($OPTIND - 1))
test $# == 0 || do_err_usage Missing argument
# Main ####################################################
#trap do_trap_user TERM INT
#trap do_trap_exit EXIT
FILEBASE="$HOME/.uptime"
TD=$HOME/bin/td.sh
test -f $TD || { echo "E: Cant find $TD. Try https://github.com/livibetter/td.sh/blob/master/td.sh"; exit 1; }
test -n "$LISTING" && {
for f in ${FILEBASE}-*
do
filedate=${f##*.uptime-}
do_debug file $f of date $filedate
echo "$filedate: $($TD $(cat $f))"
done
exit 0
}
current_uptime=$(cat /proc/uptime|awk '{print $1}'|awk -F. '{print $1}' || { echo "E: Cant use /proc/uptime!!"; exit 1; } )
old_uptime=$(cat $FILEBASE 2>/dev/null|| echo 0)
(( $current_uptime >= $old_uptime )) && {
do_print "New uptime record :) $($TD $(echo $current_uptime)) "
echo $current_uptime > $FILEBASE
} || {
do_print "reboot ;( Reset uptime records."
old_uptime_date=$(stat -c %y $FILEBASE|awk '{print $1}' 2>/dev/null || date +%Y-%m-%d)
mv $FILEBASE "$FILEBASE-$old_uptime_date"
}
# vim:set ts=4 sw=4 sta ai spelllang=en: