-
Notifications
You must be signed in to change notification settings - Fork 1
/
shell_time.sh
executable file
·42 lines (36 loc) · 1.12 KB
/
shell_time.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/bin/bash
#
# Based on http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x810.html,
# this sends a libnotify message.
#
# The use here is to remind you when you've finished with a compile
# or what-not.
#
# clocks per second is almost always this...
# Run getconf -a | grep CLK_TCK to check.
#
# To install, add the following to your .bashrc:
# SHELL_TIME_TMP_FILE=$(mktemp)
# PROMPT_COMMAND=shell_time.sh
# If you already have one, try:
# export SHELL_TIME_TMP_FILE=$(mktemp)
# PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND ; }"'shell_time.sh "$(history 1)"'
EXIT_CODE=$PREV_EXIT
# Use a temp file generated by our parent.
if [ -z $SHELL_TIME_TMP_FILE ]; then
exit 0
fi
ONE_SECOND=100
THRESHOLD=$((10 * $ONE_SECOND))
PARENT=$(cat /proc/$PPID/stat | awk '{ print ($16 + $17) }')
if [ -s $SHELL_TIME_TMP_FILE ]; then
PREV=$(cat $SHELL_TIME_TMP_FILE)
else
PREV=0
fi
ELAPSED=$(($PARENT - $PREV))
if [[ $THRESHOLD -lt $ELAPSED ]]; then
ELAPSED_SECONDS=$(($ELAPSED / $ONE_SECOND))
notify-send "Process finished ($EXIT_CODE) after ${ELAPSED_SECONDS} cpu-seconds. $1" 2> /dev/null
fi
echo $PARENT > $SHELL_TIME_TMP_FILE 2> /dev/null