Inactivity timer
Running a delayed command is simple: sleep <sleep time>; run_a_command
Wrapping that in start/stop timer functions:
INACTIVITY_TIME='5m' INACTIVITY_CMD='echo -ne \a' function inactivity-start-timer () { (sleep "$INACTIVITY_TIME"; $INACTIVITY_CMD) & INACTIVITY_PID=$! disown } function inactivity-stop-timer () { kill $INACTIVITY_PID > /dev/null 2>&1 }
You can add that your shell rc file. Now you need to run inactivity-start-timer
before every prompt and inactivity-stop-timer
before every command execution. (You don't want a beep if the command takes too long, do you?) Also this assumes you have the system bell on, otherwise put another command into INACTIVITY_CMD
.
Bash
I am guessing you use bash? In that case there is PROMPT_COMMAND to run a command before every prompt. But nothing built-in to run before every command execution. There is a known trick to get that here. So add this also to your .bashrc:
PROMPT_COMMAND=inactivity-start-timer preexec () { inactivity-stop-timer } preexec_invoke_exec () { [ -n "$COMP_LINE" ] && return # do nothing if completing [ "$BASH_COMMAND" = "$PROMPT_COMMAND" ] && return # don't cause a preexec for $PROMPT_COMMAND local this_command=`history 1 | sed -e "s/^[ ]*[0-9]*[ ]*//g"`; preexec "$this_command" } trap 'preexec_invoke_exec' DEBUG
Zsh
If you use zsh on the other hand its simpler:
autoload -Uz add-zsh-hook add-zsh-hook precmd inactivity-start-timer add-zsh-hook preexec inactivity-stop-timer