Skip to content

Instantly share code, notes, and snippets.

@ali1234
Created October 15, 2018 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ali1234/565c38bae3adc429e309b1a3100c1d6e to your computer and use it in GitHub Desktop.
Save ali1234/565c38bae3adc429e309b1a3100c1d6e to your computer and use it in GitHub Desktop.
Bash script to time a long running command and display a notification when it finishes.
#!/bin/bash
SECONDS=0
"$@"
e=$?
if [ $? -eq 0 ]; then
RESULT="Succeeded"
else
RESULT="Failed"
fi
#http://unix.stackexchange.com/a/175109/6940
seconds2time ()
{
T=$1
D=$((T/60/60/24))
H=$((T/60/60%24))
M=$((T/60%60))
S=$((T%60))
if [[ ${D} != 0 ]]
then
printf '%d days %02d:%02d:%02d' $D $H $M $S
else
printf '%02d:%02d:%02d' $H $M $S
fi
}
if (( $SECONDS > 5 )); then
NICETIME=$(seconds2time $SECONDS)
notify-send "Command $RESULT" "CMD: $*\nPID: $$\nPWD: $PWD\nTime: $NICETIME"
fi
exit $e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment