I'm aware it's already been asked how to make a sound after a command.

Despite people seem to intend to use it after long-running commands, there is initially no connexion between the actual trick and the duration of the command.

My question here is, can I (using bash or zsh) make my terminal execute a script that would wrap every command looking at start time and comparing when it finishes to end time and if and only if the command lasted more than X minutes, the sound is played?

Because in practice, those long running commands are often new and there is no chance I anticipate this every time!

Bonus: the sound should not be played for interactive commands (eg. ssh xxx), if it is too hard, the criteria could be whether the focus is given to the window or not.

Thanks a lot!

  • Short answer : no , what you ask about doesn't exist. Commented Feb 13, 2017 at 18:49
  • 1
    You can , however, make a wrapper function and append append it to each command in a script. See this answer : askubuntu.com/a/409766/295286 Commented Feb 13, 2017 at 18:52
  • @Serg what you reported is exactly what I was looking for! A sound or a desktop notification will definitely be interchangeable. Commented Feb 14, 2017 at 9:40
  • Glad my commend was useful. Just FYI: you can use aplay command instead of notify-send that is used in the other answer to play some sort of mp3 file with notification sounds. Commented Feb 14, 2017 at 9:45

1 Answer 1


Thanks for the idea, might be useful for me too in the future and was a nice excuse to learn bash, sorry for any common mistakes, just slapped this script into existence from a bunch of Google searches in less than 15 min.

Usage: script.sh "command" 29 (29 is the time in seconds) and I didn't implement the actual command to make the sound. You have to google the best option for your needs and replace it

eval $1 &
let "timer=0"
let "increase=1"
let "mtime=$2"
while true; do
    if [ "$(ps -p $PID -o pid=)" = "" ]; then exit; fi
    let "timer=timer+increase"
    if [[ timer -gt mtime ]]; then
        let "timer=0"
        let "increase=0"
    sleep 1

And no, I do not know any method to warp every command into some other, best-case-scenario you can remove the extension and put the above script somewhere in your $PATH with a short name like 'tm' to ease of access


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .