14 trackrun - Record when the given command was started and ended and expose to it in environment variables
18 trackrun [<OPTIONS>] [--] <COMMAND> [<ARGS>]
24 =item -f, --full-command
26 =item -b, --command-basename (default)
30 =item -e, --env-var ENV
36 It records when it starts COMMAND and when it ends, identifying COMMAND either by one of 4 options:
37 (1) Full command line including ARGS.
38 (2) Only the command name, COMMAND.
39 (3) By the name given by the user in NAME.
40 (4) By the environment variable given by name ENV.
42 Set B<TRACKRUN_LAST_STARTED> and B<TRACKRUN_LAST_ENDED> environments for COMMAND to the ISO 8601 representation
43 of the date and time when COMMAND was last started and ended respectively.
44 Set B<TRACKRUN_LAST_STATUS> to the status COMMAND last exited with.
45 Those are left empty if no data yet.
49 Store tracking data in F<~/.trackrun> directory.
55 =item TRACKRUN_LAST_STARTED
57 =item TRACKRUN_LAST_ENDED
59 =item TRACKRUN_LAST_STATUS
65 Trackrun does not do locking. You may take care of it if you need using flock(1), cronrun(1), or similar.
85 -b|
--command-basename)
102 echo "$0: unknown option: $1" >&1
118 declare -a cmd_args
=("$@")
125 cmd_id
=${cmd_args[*]}
128 cmd_id
=`basename "${cmd_args[0]}"`
134 track_dir
=~
/.trackrun
135 cmd_id_hash
=`printf %s "$cmd_id" | md5sum | cut -c 1-32`
136 track_file
=$track_dir/$cmd_id_hash
141 # return the C-th field of the first line in stdin of which A-th field is B
142 awk -F $
'\t' -v field_num
="$1" -v field_val
="$2" -v field_ret
="$3" \
143 '{if($field_num == field_val){ print $field_ret; exit; }}'
146 mkdir
-p "$track_dir"
147 export TRACKRUN_LAST_STARTED
=`tac "$track_file" 2>/dev/null | lookup 1 start 2`
148 export TRACKRUN_LAST_ENDED
=`tac "$track_file" 2>/dev/null | lookup 1 end 2`
149 export TRACKRUN_LAST_STATUS
=`tac "$track_file" 2>/dev/null | lookup 1 end 3`
153 printf "%s\t%(%FT%T%z)T\t%s\t%s\n" "$1" -1 "${2:-}" "${cmd_args[*]}" >> "$track_file"
159 exec -- setsid
${cmd_args[@]}
164 signal_handler
() { interrupted
=yes; }
165 trap signal_handler INT
176 kill -s INT
$child_pid || true
182 record end
$exit_code