Make a beep sound when console outputs certain text
| Lastmod: 2019-09-12
Originally this was a question posed on the hugo discussion forums.
hugo server, the user wanted to be notified with a beep of any error messages that were output by the console.
This isn’t a use case for me personally but I thought it was a fun problem to solve.
My original solution for Mac:
hugo server 2>&1 | tee /dev/tty | while read line ; do echo $line | say --rate=500 ; done
hugo server 2>&1 – Redirect
stdout, then pipe
tee /dev/tty – Pipe
stdout while still showing it in the console
while read line ; do echo $line – Echo each line, then pipe it
say --rate=500 – Use the Mac
say command to read from
stdin at a rate of 500 words per minute
The user then mentioned he was on Windows, so I tweaked it and gave him additional instructions:
- Download and install git bash
- Download voice.exe then add it to your
In a git bash window run:
hugo server 2>&1 | tee /dev/tty | while read line ; do echo $line | if [[ $line = *"ERROR"* ]] ; then voice.exe --rate=10 ; fi ; done
if [[ $line = *"ERROR"* ]] ; then voice.exe --rate=10 ; fi – This time, only speak the line if it contains the text
ERROR. And since it’s Windows, use
voice.exe at its fasted rate of speech,
Finally, after more research, I came up with this solution to address the original question:
hugo server 2>&1 | tee /dev/tty | while read line ; do echo $line | if [[ $line = *"ERROR"* ]] ; then rundll32 user32.dll,MessageBeep ; fi ; done
rundll32 user32.dll,MessageBeep – Instead of speaking the line, actually make a “beep” sound by using the Windows MessageBeep function
See Windows Rundll and Rundll32 interface for more info.