Skip to content

Instantly share code, notes, and snippets.

@vmarkovtsev
Last active January 21, 2021 12:52
Show Gist options
  • Save vmarkovtsev/3e0f910a7c38fc5da23723d89dffe39c to your computer and use it in GitHub Desktop.
Save vmarkovtsev/3e0f910a7c38fc5da23723d89dffe39c to your computer and use it in GitHub Desktop.
How to configure bash to notify finished commands when unfocused

Open ~/.bashrc. Append export WINDOWID=... in the header as follows:

# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

export WINDOWID=$(xdotool getactivewindow)

Locate alias alert and change it as follows, prepending to PS1:

alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(dateutils.ddiff $(HISTTIMEFORMAT="%FT%T " history 1|cut -d\  -f3) $(date +%FT%T))" "$(HISTTIMEFORMAT= history 1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'

PS1='$(if [ "$(xdotool getwindowfocus 2>/dev/null)" != "$WINDOWID" ]; then alert; fi)'"$PS1"

Install xdotool:

sudo apt install xdotool

Test: open a new terminal session, execute sleep 5, then switch to your web browser or any other window. You should see a system notification.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment