13 trackrun - Record when the given command was started and ended and expose to it in environment variables
17 trackrun [I<OPTIONS>] [--] I<COMMAND> [I<ARGS>]
21 It records when it starts COMMAND and when it ends, identifying COMMAND either by one of 4 options:
25 =item Full command line including I<ARGS>.
27 =item Only the command name, I<COMMAND>.
29 =item By the name given by the user in I<NAME>.
31 =item By the environment variable value given by name I<ENV>.
35 Set B<TRACKRUN_LAST_STARTED> and B<TRACKRUN_LAST_ENDED> environments for COMMAND to the ISO 8601 representation
36 of the date and time when COMMAND was last started and ended respectively.
37 Set B<TRACKRUN_LAST_STATUS> to the status COMMAND last exited with.
38 Those are left empty if no data yet.
40 On every run, a UUID is generated, so you can connect events of concurrent runs in the track report.
41 It is exposed in B<TRACKRUN_UUID> env.
47 =item -f, --full-command
49 =item -b, --command-basename (default)
51 =item -n, --name I<NAME>
53 =item -e, --env-var I<ENV>
57 Show the hash generated from either of those options above before run the I<COMMAND>.
58 This hash is used for the filename in which command-related events are stored.
62 Show the current run's UUID before actually start the command.
64 =item -U, --write-uuid I<FILE>
66 Write current run's UUID in the given file before start the command.
70 Do not run I<COMMAND>, instead display its tracked history.
76 Store tracking data in F<~/.trackrun> directory.
82 =item TRACKRUN_LAST_STARTED
84 =item TRACKRUN_LAST_ENDED
86 =item TRACKRUN_LAST_STATUS
88 =item TRACKRUN_LAST_UUID
90 =item TRACKRUN_LAST_SUCCESSFUL_STARTED
92 =item TRACKRUN_LAST_SUCCESSFUL_ENDED
94 =item TRACKRUN_LAST_SUCCESSFUL_UUID
96 The last successful run's UUID, date-time when started and ended.
100 The current run's UUID
106 Trackrun does not do locking. You may take care of it if you need using flock(1), cronrun(1), or similar.
130 (-b|
--command-basename)
160 echo "$0: unknown option: $1" >&1
169 declare -a cmd_args
=()
185 cmd_id
=${cmd_args[*]}
188 cmd_id
=`basename "${cmd_args[0]}"`
191 echo "$0: empty --name" >&2
195 echo "$0: value empty in --env-var" >&2
202 track_dir
=~
/.trackrun
203 cmd_id_hash
=`printf %s "$cmd_id" | md5sum | cut -c 1-32`
204 track_file
=$track_dir/$cmd_id_hash
214 cat "$track_file" |
tac | td-add-headers UUID EVENT DATETIME STATUS COMMAND
221 # lookup A B [A B [A B]]
222 # search a line in STDIN of which field A's value is B for all pair of A and B,
223 # if A is not a number but "return", then return the B field's value.
224 # Fields are counted form zero.
225 perl
-e 'while(<STDIN>) {
231 $filter_field = shift @Filters;
232 $filter_value = shift @Filters;
233 if($filter_field eq "return") { print $filter_value eq "record" ? $_ : $field[$filter_value]; exit; }
234 elsif($field[$filter_field] ne $filter_value) { last; }
239 mkdir
-p "$track_dir"
241 last_record_end
=`tac "$track_file" 2>/dev/null | lookup 1 end return record`
242 export TRACKRUN_LAST_UUID
=`echo "$last_record_end" | lookup return 0`
243 export TRACKRUN_LAST_STARTED
=`tac "$track_file" 2>/dev/null | lookup 0 "$TRACKRUN_LAST_UUID" 1 start return 2`
244 export TRACKRUN_LAST_ENDED
=`echo "$last_record_end" | lookup return 2`
245 export TRACKRUN_LAST_STATUS
=`echo "$last_record_end" | lookup return 3`
246 last_successful_record_end
=`tac "$track_file" 2>/dev/null | lookup 1 end 3 0 return record`
247 export TRACKRUN_LAST_SUCCESSFUL_UUID
=`echo "$last_successful_record_end" | lookup return 0`
248 export TRACKRUN_LAST_SUCCESSFUL_STARTED
=`tac "$track_file" 2>/dev/null | lookup 0 "$TRACKRUN_LAST_SUCCESSFUL_UUID" 1 start return 2`
249 export TRACKRUN_LAST_SUCCESSFUL_ENDED
=`echo "$last_successful_record_end" | lookup return 2`
252 if [ -z "${cmd_args[0]:-}" ]
257 type uuidgen
>/dev
/null
258 export TRACKRUN_UUID
=`uuidgen`
262 echo "${0##*/}: command run uuid: $TRACKRUN_UUID" >&2
264 if [ -n "$uuid_outfile" ]
266 echo "$TRACKRUN_UUID" > "$uuid_outfile"
271 printf "%s\t%s\t%(%FT%T%z)T\t%s\t%s\n" "$TRACKRUN_UUID" "$1" -1 "${2:-}" "${cmd_args[*]}" >> "$track_file"
277 exec -- setsid
"${cmd_args[@]}"
282 signal_handler
() { interrupted
=yes; }
283 trap signal_handler INT
294 kill -s INT
$child_pid || true
300 record end
$exit_code