-
Notifications
You must be signed in to change notification settings - Fork 13
/
age-of
executable file
·66 lines (54 loc) · 1.18 KB
/
age-of
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
#!/bin/sh
#
# Show the age in seconds of a running or completed command which
# was run under the age-track wrapper. exit 0 if still running, 1 otherwise.
AGE_DIR="$HOME/.run/age"
me=`basename $0`
usage () {
cat <<EOF >&2
Usage: $me [-n] COMMAND [ARGS]
-n Calculate age as if process was still running
EOF
exit 1
}
if [ "$1" == -h ] || [ "$1" == --help ] || [ $# -eq 0 ]; then
usage
fi
if [ "$1" = -n ]; then
now=y
shift
fi
cmd="$1"
shift
base_cmd=`basename "$cmd"`
cmd_dir="$AGE_DIR/$base_cmd"
start_file="$cmd_dir/start"
end_file="$cmd_dir/end"
if ! [ -d "$cmd_dir" ]; then
echo "$cmd_dir does not exist; was the command run under age-track?" >&2
exit 1
fi
if ! [ -e "$start_file" ]; then
echo "$start_file does not exist; aborting." >&2
exit 1
fi
start=$( cat "$start_file" )
if [ -e "$end_file" ]; then
end=$( cat "$end_file" )
exit=1 # command not running
if [ "$end" -lt "$start" ]; then
echo "Command still running." >&2
end=$( date +"%s" )
exit=0
fi
else
# no end file, must still be running
echo "Command still running." >&2
end=$( date +"%s" )
exit=0
fi
if [ -n "$now" ]; then
end=$( date +"%s" )
fi
echo $(( end - start ))
exit $exit