From the Canyon Edge -- :-Dustin

Friday, January 1, 2010

Byobu Custom Status Notifications in 3 Easy Steps!

Richard Johnson blogged a really cool hack for Byobu a couple of days ago. He created a custom script to retrieve his next appointment from his calendar and display it in Byobu's status bar. This is really cool ;-)

Many Byobu users have asked me for this sort of functionality over the last year -- the ability to add custom status items to the lower bar. I'm pleased to announce that this is now possible as of Byobu 2.40+!

This version is already in Lucid, but if you're not yet running Lucid, you can grab the latest backported package from the Byobu PPA:
After upgrading Byobu, you can create your own custom script, in 3 easy steps!

I run a lot of KVM virtual machines on my Ubuntu servers, usually through the Ubuntu Enterprise Cloud and Eucalyptus. At any given time, when I log onto a Eucalyptus Node and automatically launch Byobu, I'd like to know how many virtual machines are running on that system. So I create a simple shell script that does something like this -- grep the process table for kvm processes and echo a count of VMs running.

Now, let's add this to my Byobu status bar!

1. Create your Byobu bin directory:
mkdir -p $HOME/.byobu/bin

2. Create your script, naming it NN_foo, where NN is the number in seconds that you want to run your script, and foo is some description of your status item:
cat > $HOME/.byobu/bin/10_vms <<EOF
#!/bin/sh
echo "VMs:\$(pgrep -c kvm)"
EOF
cat $HOME/.byobu/bin/10_vms

Note that I called this script "10_vms". The number "10" is the frequency in seconds you want Byobu to refresh this status item. You need to be careful here, making sure that your status program doesn't gobble too much CPU! Choose this number carefully, and consider how often the information displayed is likely to change. I recommend running "time" on your script a few times, seeing how long it actually takes to run. My example is trivial, and executes within 0.003 seconds on my system, so running it every 10 seconds is still relatively inexpensive. Your script might be much more complex, and may not need to be run as frequently!

3. Make your script executable:
chmod +x $HOME/.byobu/bin/10_vms

And within a couple of seconds, it should show up in your status bar!


Related hacks:
  • You can put as many of these as you want in your $HOME/.byobu/bin.
  • You can enable/disable each by changing the permissions, adding or removing the executable bit (chmod +x or chmod -x).
  • You can disable all of your custom scripts in the Byobu Menu with:
    F9 -> Toggle status notifications -> Disable "custom" -> Apply
  • You can write your custom script or program in any language, as long as it's executable and emits something on stdout.
  • You can even add support for the "Byobu Status Detail" feature (see ctrl-a-$), if you add a handler for a "--detail" command line option. e.g. for my VM script, I could add the following line:
    [ "$1" = "--detail" ] && ps -ef | grep kvm

If you find this useful, and create some cool, custom scripts, please post a comment! -- Ideally with a link to your script's source code, and a GPLv3 copyright header ;-)

Happy New Year!

:-Dustin