Archive for the ‘bash’ Category

Notify me when the script is done

Do you know the following situation ?
You’ve done some work and, before commiting, you switch to the shell in order to start the testsuite which usually runs a couple of minutes (blessed are those projects where it is not hours).
Now you got different choices what to do in the meantime. Sometimes i find me reading news and either completely forget about the running tests or i switch continoulsy back and forth between the browser and the shell. Both can be annoying, especially if you read news for 10 minutes and then find your testsuite failed after 10 seconds. What can help ? Proper Notification!

So here is a notification technique i assembled for my environment. Its based on mac, iterm and growl but it should be applyable to other unix environments as well. It simply fires a visual or audi-visual notification for every command which takes more then 10 seconds to complete when it is complete.

Setup Growl

Get Growl and ITerm, if you havn’t yet. Growl its an notification system which integrates with all kind of applications like Mail or Skype. Once you’ve installed growl, you can configure it through the growl preference pane in System Preferences. Enable general ITerm notifications and make sure the specialized iterm notification ‘Customized Message’ is enabled as well. You might want to add a sound to this notification as well.
(There are bunch of standard notifications for iterm but i have found none of these working very well for the described purpose, so i found me rather disabling most of them.)

Setup Bash

You can fire growl notifications right from the bash to growl (see this blog post). To easify this add the following line to your .bash_profile:

growl() { echo -e $'\e]9;'${1}'07' ; return  ; }

Now you can fire notifications from iterm simply by typing:

$growl "hello world"

The only missing logic can be crafted in two lines of the .bash_profile as well.
Set a trap which sets the startTime for a command once you execute one:

trap 'if [ ! -n "$initTrap" ]; then startTime=`date +%s`; commandName=${BASH_COMMAND}; fi; initTrap="true"' DEBUG

And set a prompt-command which will be executed once the executed command returns.

export PROMPT_COMMAND='if [ $? == 0 ];then exitCode='OK'; else exitCode='ERROR'; fi;now=`date +%s`; duration=`expr $now - $startTime`; startTime=$now; echo "took $duration sec, $exitCode";if [ $duration -gt 10 ]; then dir=`pwd`; growl "\n ${commandName} \ntook $duration sec \n $exitCode";fi; unset initTrap'

With these settings every command which takes longer then 10 seconds fires of an growl notification which may include a sound. You can adjust those 10 seconds however you want, practically i found them working very well!

BTW when completing this blog post i found a similar one, with even the same kind of introduction, funny… Question: why did i find that now ? Weeks after i put the above solution together with sweat on my face ? )

And this is how it could look like: