Search code examples
macosaudiocommandalarm

How can we play sound alarm after command is done on Mac terminal?


When we run a command that takes a while such as source compile or brew upgrade, we usually do other things such as web searching rather than watching the command running the whole time.

Is there any possible way that I can get notified once the command is done?

brew upgrade | afplay /System/Library/Sounds/Submarine.aiff -v 10

I tried with afplay on Mac as above but the problem is a) stdout is not shown due to the pipe, b) it plays the sound right after the command starts to run rather than waiting until "brew upgrade" is finished.

Any suggestions please?


Solution

  • I think you need

    brew upgrade && afplay /System/Library/Sounds/Submarine.aiff -v 10
    

    then the afplay will run if the brew completes successfully.

    As a note of explanation, you are asking the shell to determine if both statements ("brew" and "afplay") are true - it is therefore obliged to evaluate the "brew" statement fully (i.e. wait for it to finish) before executing the "afplay" in order to determine whether both statements are true.