2008-01-11

Freaky Friday Unix tip of the day: Named Pipes

Named pipes, also called FIFO (First In/First Out) Buffers, are a simple way to get piped data into a command that is expecting to read data from a file, for simulated local client/server communication, or to queue up data for periodic processing such as with cron.


To create a named pipe, use the mkfifo command.  The only options for mkfifo are -m and the filename.  If invoked without a mode for the newly created named pipe, the current umask is used as usual.  For example, an octal umask of 022 will be used to create files with permissions of 755 (rwx,r-x,r-x).

I like to use root-tail to put status messages on my graphical desktop.  Root-tail compiles and runs on pretty much every UNIX flavor that handles X11, but it can't accept input from a pipe, though.  It basically does a tail -f on a file. 

One such status message I like is a periodic ping so I know if I'm still up on the wireless network.  OpenBSD doesn't exactly give you an easy wireless-strength meter, nor do I care enough to see if there are any utilities out there to do it.  So, I made a fifo for my ping output:

$ mkfifo /tmp/pingfifo

Then I started root-tail in the background (see the root-tail manpage for more info on configuration screen positioning):

$ nohup root-tail -i 1 -g 500x120+700+610 /tmp/pingfifo,red&

And finally, I launched my ping script and redirected it to the fifo, also in the background:


ping.sh


#!/bin/sh
while true
do
ping -c4 www.google.com
sleep 38
done



$ nohup ./ping.sh > /tmp/pingfifo&

The end result is a transparent text box on my screen that shows me what I want (under the needle of the turntable, on the far right):



Note:
Root-tail works great on log files as-is if you wish to use it that way. This quick snippit was written to demonstrate how Unix allows pipes to be created to act like files for commands such as root-tail that only deal with files.

See Also:
Sysadmin Sunday
root-tail home page

blog comments powered by Disqus