2

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!

1 Answers1

0

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

#!/bin/bash
eval $1 &
PID=$!
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"
        replaceme-with-sound-command
    fi
    sleep 1
done

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

  • Thanks for the try. Look at @Serg link, it seems really helpful. If you are on a learning dynamic, I'd encourage you to have a look at zsh callbacks just like this question I asked: http://stackoverflow.com/questions/30169090/zsh-behavior-on-enter – Augustin Riedinger Feb 14 '17 at 09:43