1

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!

4
  • Short answer : no , what you ask about doesn't exist. – Sergiy Kolodyazhnyy Feb 13 '17 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 – Sergiy Kolodyazhnyy Feb 13 '17 at 18:52
  • @Serg what you reported is exactly what I was looking for! A sound or a desktop notification will definitely be interchangeable. – Augustin Riedinger Feb 14 '17 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. – Sergiy Kolodyazhnyy Feb 14 '17 at 9:45
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

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.