OS X: Audio Alerts for PHP Errors
Posted Jun 22nd, 2010 by Conor in in Apple,Apple Script,Bash,PHPKae posted today about tackling this problem under Linux, but I’ve found that it’s quite a different task under OS X. Anyway here is how it’s done:
First you need to download the swatch package from here.
If you haven’t done it before you will have to set up cpan and it’s a lengthy process. Run cpan in the terminal. It’s safe to say yes to all the options. Once it’s installed you need to add some perl modules. In the cpan prompt add the following modules:
install Date::Calc install Date::Format install Date::Manip install File::Tail
That’s the perl part finished anyway. Now change to the swatch directory and compile:
perl Makefile.pl make make install
Ok swatch should be installed now. So set up the configuration file: vim ~/.swatchrc Add the following lines to the file:
watchfor /PHP Parse error|PHP Fatal error/ bell 3
Save and return to the command line. Now to add swatch (and also optionally apache and mysql) as a startup item do as follows. Create a new directory named StartUpItems, then create the following files so your setup should look like this:
StartUpItems StartUpItems/StartUpItems (a file with no extention) StartUpItems/StartupParameters.plist
Now add the following contents to StartupParameters.plist:
< ?xml version="1.0" encoding="UTF-8"?> < !DOCTYPE plist SYSTEM "file://localhost/System/Library/DTDs/PropertyList.dtd">Description Various Startup commands including Swatch, Apache and MySQL. OrderPreference Late Provides Starts up Swatch, Apache and MySQL.
That’s just a file required by OS X to recognise the startup item, don’t worry to much about it. Now to the StartUpCommands file which is a bash script:
#!/bin/bash
. /etc/rc.common
StartService(){
ConsoleMessage "Starting Apache, MySQL and Swatch"
apachectl start
/usr/local/mysql/support-files/mysql.server start
/opt/local/bin/swatch --daemon --config-file=/Users/conormacaoidh/.swatchrc --tail-file=/var/log/php/php-error_log --pid-file=/var/run/swatch-httpd-errors.pid
}
StopService(){
ConsoleMessage "Stopping Apache, MySQL and Swatch"
PID=`cat /var/run/swatch-httpd-errors.pid`
kill -9 $PID
apachectl stop
/usr/local/mysql/support-files/mysql.server stop
}
RestartService(){
RunService stop
RunService start
}
RunService "$1"
The file
/etc/rc.common
is provided by apple and makes the process a lot easier, though it is not needed. The lines in StartService and StopService do the opposite of each other but are very similar. You may need to change the apache and mysql start/stop commands according to your own setup. If you are using MAMP this file will start apache and mysql:
/Applications/MAMP/bin/start.sh
As for the swatch line you will obviously have to change the config file location and the php error log location to what’s appropriate on your machine.
And that’s it really. Then copy the items to the correct directory for OS X to recognise them:
sudo cp -R StartUpCommands /Library/StartUpItems
It is vital that you change the permissions or else the startup item simply won’t be executed:
sudo chown -R root /Library/StartUpItems/StartUpCommands sudo chgrp -R wheel /Library/StartUpItems/StartUpCommands sudo chmod -R 755 /Library/StartUpItems/StartUpCommands
That’s it. You can test the configuration by running SystemStarter -n -D which will emulate what happens when the computer starts, it is useful for de-bugging.
So restart you computer, load a php file with an error in it and your machine will beep thrice!