22 Jun

audio alerts for php errors

I tend to keep a log tailing in one console while working in another.

tail is a Linux program that displays the last few lines of a file. If you run tail -f /var/log/httpd/error_log as root, or as your normal user if you’ve set the right permissions, then you will see any errors as they are added to the log.

However, I also tend to get immersed in my coding and not notice any errors until they cause a visual problem.

What I needed was a program that would watch my log, and beep to get my attention if an error was added.

This article explains how to set that up in Fedora.

First, install the program swatch:

[root@ryuk ~]# yum install swatch

Now create /etc/init.d/swatch, which is the startup/shutdown script for the logger:

#!/bin/sh

case "$1" in
'start')
                /usr/bin/swatch --daemon --config-file=/etc/swatch-httpd-errors --tail-file=/var/log/httpd/error_log --pid-file=/var/run/swatch-httpd-errors.pid
                ;;
'stop')
                PID=`cat /var/run/swatch-httpd-errors.pid`
                kill -9 $PID
                ;;
*)
                echo "Usage: $0 { start | stop }"
                ;;
esac
exit 0

Notice the two file locations in the /usr/bin/swatch command – the configuration file location, and the log file to be tailed.

Make the file executable:

[root@ryuk ~]# chmod +x /etc/init.d/swatch

Now create the configuration file, /etc/swatch-httpd-errors:

watchfor /PHP Parse error|PHP Fatal error/
        exec mplayer /home/kae/sounds/beep-8.wav > /dev/null 2> /dev/null

You can have swatch do basically anything, from sending an email, to flashing lights in your face if you have them connected to the computer. All I wanted was a little beep.

Change the exec command to whatever your want.

I started experimenting by having Arnold Schwarzenegger yell “What the hell are you doing??” at me, but that could get annoying for other people. In the end, I changed it to a beep – a very /short/ beep. More of a tick than a beep.

Now start up the daemon:

[root@ryuk ~]# /etc/init.d/swatch start

Create a php script with an error in it, and view it in your browser. You should get a satisfying beep.

If you want to have this run automatically when the laptop boots, add the above command to the /etc/rc.local file.

3 thoughts on “audio alerts for php errors

  1. that’s exactly what I need.. Let’s see how I get on doing that on OSX. It would be interesting to get the computer to yell out the line number and file name as well though that could also get annoying..

  2. Pingback: Conor's Blog

Leave a Reply to kenCancel reply