Hack and / - Message for You Sir
It's easy to forget dæmons are there unless they demand your attention. A few years ago, I was walking through the expo floor at OSCON, when I noticed someone in a full BSD dæmon costume getting his picture taken with a few fans. When I saw them trying to figure out how to arrange everyone for the picture I couldn't help but yell, “No! The dæmon is always in the background!”
In case you didn't get the joke, dæmon is a name UNIX people give to processes that run behind the scenes (in the background). Dæmons perform all sorts of useful functions from executing scripts at a certain time (atd and crond) to listening for network connections and spawning the appropriate process to serve the request (inetd). In fact, the d at the end of those scripts stands for dæmon, and you might notice that a number of processes on your system right now end in d.
The whole point of a dæmon is to perform tasks without your intervention or knowledge, but sometimes, it's handy for a dæmon to alert you when a certain condition occurs. On a server, this usually means the dæmon will send an e-mail alert to the administrator, but what about on a desktop? What if you want a dæmon to alert you when you have new e-mail? In that case, it makes more sense for some sort of notice to pop up on your desktop. In this column, I discuss three different methods I use so that dæmons can get my attention on my desktop.
I think the first time I noticed OSD (On-Screen Display) notifications on Linux was with a volume control program. I increased the volume on my computer, and right in the middle of the screen was a volume meter floating above all my other windows, just like on a TV. I instantly was intrigued and had to figure out how they did it. These days, there are a number of different OSD libraries and programs, but my favorite is still osd_cat.
The osd_cat program is a command-line program that displays text sent to it in a pipe. The fact that it accepts piped input makes it ideal for dæmon notification, because it's easy to add to any shell script. This command is part of the xosd-bin package on Debian-based systems, or xosd on Red Hat, and has been around for a number of years.
The simplest way to test osd_cat is to pipe some text to it:
$ echo "Hello World" | osd_cat
If you look at the top left-hand side of your screen, you should see your message appear in a small red font for a few seconds and then disappear. Of course, if you didn't know to look there, you might assume the program is broken because the message is so small. Plus, everyone knows green is the ideal foreground color, so let's spruce up that notification a bit and put it front and center:
$ echo "Hello World" | osd_cat --align=center --pos=bottom ↪--color=green --font=lucidasanstypewriter-bold-24
Ahh, that's more like it, a notification right in the middle of the screen. As you can see, osd_cat accepts a number of options that can control how and where it displays your message. The man page covers all the options in detail, but I highlight the options I used here. The --align argument controls the text alignment much like a word processor and can be set to left (default), center or right. The --pos option controls the Y orientation on the screen and can be set to top (default), middle or bottom. The --color option is self-explanatory, as is --font. If you do want a different font but aren't sure what value to use, just run xlsfonts to see a full list.
In addition to the options I listed, osd_cat has many other options, such as --indent and --offset, that allow you to fine-tune where it displays your text, so you can position it virtually anywhere on the screen. You also can tweak the time the message appears on the screen with the --delay option, and if you plan to pipe multiple lines of text to the screen, be sure to look into the --lines, --age and --wait options, so you can control how osd_cat will scroll multiple lines. There are even --barmode and --percentage options that let you draw a slider bar, much like the OSD volume control I saw.
All of those options are nice, but honestly, I find myself sticking to basic text notifications with osd_cat, and although I have migrated many of my scripts to libnotify, I still like to use osd_cat for audio/video notification, such as when I use a script I wrote to turn on the VGA output on my laptop for presentations. I mentioned this script in my original Lightning Hacks column, but in case you didn't see it, here it is again:
#!/bin/sh if xrandr | grep -q 'VGA connected'; then echo "LVDS + VGA" | osd_cat --shadow=2 --align=center ↪--pos=bottom --color=green --delay=2 ↪--font=lucidasanstypewriter-bold-24 --offset 40 & # choose my laptop screen's resolution by default; # if that fails, try the auto-detected mode xrandr --output VGA --mode 1280x768@60 || xrandr ↪--output VGA --auto else echo "LVDS only" | osd_cat --shadow=2 --align=center ↪--pos=bottom --color=green --delay=2 ↪--font=lucidasanstypewriter-bold-24 --offset 40 & xrandr --output VGA --off & fi
I find it's nice to add visual notifications like this whenever I trigger a script in the background with a keybinding and it's not immediately obvious the script ran, such as in this case, when I'd like to know whether I'm in presentation or regular mode, and it might take the projector a few seconds to respond.
Kyle Rankin is VP of engineering operations at Final, Inc., the author of many books including Linux Hardening in Hostile Networks, DevOps Troubleshooting and The Official Ubuntu Server Book, and a columnist for Linux Journal. Follow him @kylerankin
|Graph Any Data with Cacti!||Apr 27, 2017|
|Be Kind, Buffer!||Apr 26, 2017|
|Preparing Data for Machine Learning||Apr 25, 2017|
|openHAB||Apr 24, 2017|
|Omesh Tickoo and Ravi Iyer's Making Sense of Sensors (Apress)||Apr 21, 2017|
|Low Power Wireless: 6LoWPAN, IEEE802.15.4 and the Raspberry Pi||Apr 20, 2017|
- Graph Any Data with Cacti!
- Teradici's Cloud Access Platform: "Plug & Play" Cloud for the Enterprise
- The Weather Outside Is Frightful (Or Is It?)
- Simple Server Hardening
- Understanding Firewalld in Multi-Zone Configurations
- Gordon H. Williams' Making Things Smart (Maker Media, Inc.)
- Server Technology's HDOT Alt-Phase Switched POPS PDU
- IGEL Universal Desktop Converter
- From vs. to + for Microsoft and Linux