14 trackrun - Record when the given command was started and ended and expose to it in environment variables
18 trackrun [I<OPTIONS>] [--] I<COMMAND> [I<ARGS>]
22 It records when it starts COMMAND and when it ends, identifying COMMAND either by one of 4 options:
26 =item Full command line including I<ARGS>.
28 =item Only the command name, I<COMMAND>.
30 =item By the name given by the user in I<NAME>.
32 =item By the environment variable value given by name I<ENV>.
36 Set B<TRACKRUN_LAST_STARTED> and B<TRACKRUN_LAST_ENDED> environments for COMMAND to the ISO 8601 representation
37 of the date and time when COMMAND was last started and ended respectively.
38 Set B<TRACKRUN_LAST_STATUS> to the status COMMAND last exited with.
39 Those are left empty if no data yet.
41 On every run, a UUID is generated, so you can connect events of concurrent runs in the track report.
42 It is exposed in B<TRACKRUN_UUID> env.
48 =item -f, --full-command
50 =item -b, --command-basename (default)
52 =item -n, --name I<NAME>
54 =item -e, --env-var I<ENV>
58 Show the hash generated from either of those options above before run the I<COMMAND>.
59 This hash is used for the filename in which command-related events are stored.
63 Show the current run's UUID before actually start the command.
65 =item -U, --write-uuid I<FILE>
67 Write current run's UUID in the given file before start the command.
71 Do not run I<COMMAND>, instead display its tracked history.
77 Store tracking data in F<~/.trackrun> directory.
83 =item TRACKRUN_LAST_STARTED
85 =item TRACKRUN_LAST_ENDED
87 =item TRACKRUN_LAST_STATUS
95 Trackrun does not do locking. You may take care of it if you need using flock(1), cronrun(1), or similar.
119 -b|
--command-basename)
149 echo "$0: unknown option: $1" >&1
165 declare -a cmd_args
=("$@")
172 cmd_id
=${cmd_args[*]}
175 cmd_id
=`basename "${cmd_args[0]}"`
181 track_dir
=~
/.trackrun
182 cmd_id_hash
=`printf %s "$cmd_id" | md5sum | cut -c 1-32`
183 track_file
=$track_dir/$cmd_id_hash
188 cat "$track_file" |
tac | td-add-headers UUID EVENT DATETIME STATUS COMMAND
196 # return the C-th field of the first line in stdin of which A-th field is B
197 awk -F $
'\t' -v field_num
="$1" -v field_val
="$2" -v field_ret
="$3" \
198 '{if($field_num == field_val){ print $field_ret; exit; }}'
201 mkdir
-p "$track_dir"
202 export TRACKRUN_LAST_STARTED
=`tac "$track_file" 2>/dev/null | lookup 1 start 3`
203 export TRACKRUN_LAST_ENDED
=`tac "$track_file" 2>/dev/null | lookup 1 end 3`
204 export TRACKRUN_LAST_STATUS
=`tac "$track_file" 2>/dev/null | lookup 1 end 4`
206 type uuidgen
>/dev
/null
207 export TRACKRUN_UUID
=`uuidgen`
211 echo "${0##*/}: command run uuid: $TRACKRUN_UUID" >&2
213 if [ -n "$uuid_outfile" ]
215 echo "$TRACKRUN_UUID" > "$uuid_outfile"
220 printf "%s\t%s\t%(%FT%T%z)T\t%s\t%s\n" "$TRACKRUN_UUID" "$1" -1 "${2:-}" "${cmd_args[*]}" >> "$track_file"
226 exec -- setsid
${cmd_args[@]}
231 signal_handler
() { interrupted
=yes; }
232 trap signal_handler INT
243 kill -s INT
$child_pid || true
249 record end
$exit_code