image of a red flower Open main menu Close main menu

Healthlad.com

Go

Make a beep sound when console outputs certain text

2018/09/18

Originally this was a question posed on the hugo discussion forums.

When running 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

Explained:

hugo server 2>&1 – Redirect stderr to stdout, then pipe stdout

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:

  1. Download and install git bash
  2. Download voice.exe then add it to your PATH
  3. 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

Explained:

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, 10


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

Explained:

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.