-
Notifications
You must be signed in to change notification settings - Fork 13
/
age-track
executable file
·58 lines (44 loc) · 1.03 KB
/
age-track
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
#!/bin/sh
#
# Track the start and end times of a command, ensuring against
# concurrent invocations. Useful as a helper for asynchronous
# process invocations.
AGE_DIR="$HOME/.run/age"
me=`basename $0`
. $ZDOTDIR/lib/libproc.sh
usage () {
cat <<EOF >&2
Usage: $me [-n NAME] COMMAND [ARGS]
EOF
exit 1
}
if [ "$1" == -h ] || [ "$1" == --help ] || [ $# -eq 0 ]; then
usage
fi
if [ "$1" == -n ]; then
name="$2"
shift 2
fi
cmd="$1"
shift
base_cmd=`basename "$cmd"`
cmd_dir="$AGE_DIR/${name:-$base_cmd}"
mkdir -p "$cmd_dir"
lock="$cmd_dir/lock"
clean_up () {
date +"%s" > "$cmd_dir/end"
rm "$lock" || echo "$me: WARNING: rm $lock failed" >&2
}
obtain_lock "$lock" "$cmd" || exit 1
# Signal must be trapped *after* obtaining lock, otherwise
# failure to obtain the lock would remove an active lock.
trap clean_up EXIT
echo "$*" > "$cmd_dir/args"
date +"%s" > "$cmd_dir/start"
[ -e "$cmd_dir/exit_code" ] && rm -f "$cmd_dir/exit_code"
"$cmd" "$@" &
child_pid=$!
wait $child_pid
exit=$?
echo $exit > "$cmd_dir/exit_code"
exit $exit