Skip to content

Instantly share code, notes, and snippets.

@jamo
Forked from matti/shell-exec-time.sh
Created April 8, 2018 09:13
Show Gist options
  • Save jamo/eae7ab7fd89c2a83c9cbada0ba49ec3f to your computer and use it in GitHub Desktop.
Save jamo/eae7ab7fd89c2a83c9cbada0ba49ec3f to your computer and use it in GitHub Desktop.
### Display execution time if command took longer than REPORTTIME_TOTAL
#
# https://superuser.com/a/847411/376927
#
REPORTTIME_TOTAL=1 # seconds
cmd_execution_time() {
local stop=$((`date "+%s"`))
let local "cmd_elapsed = ${stop} - ${cmd_start_time}"
let local "cmd_minutes = ${cmd_elapsed} / 60"
let local "cmd_seconds = ${cmd_elapsed} % 60"
(( $cmd_elapsed > $REPORTTIME_TOTAL )) && print -P "%F{yellow}${cmd_minutes} min ${cmd_seconds} sec%f"
}
preexec() {
cmd_start_time=$((`date "+%s"`))
}
precmd() {
if (($+cmd_start_time)); then
cmd_execution_time
fi
}
###
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment