Friday, January 28, 2011

Make your own daemon

A fantastic article on swatch (http://www.linux-mag.com/id/7807) included this delightfully simple framework for rolling your own daemons. If you take a moment to read it, you'll see a lot of unix design principles relating in perfect harmony:

#!/bin/sh 
# Simple Log Watcher Program  
case "$1" in 'start')   
/usr/bin/swatch --daemon --config-file=/etc/swatch.conf --tail-file=/var/log/auth.log --pid-file=/var/run/swatch.pid   
;; 
'stop')   PID=`cat /var/run/swatch.pid`   kill $PID   
;; 
*)   echo "Usage: $0 { start | stop }   
;; 
esac 
exit 0 

Thursday, January 27, 2011

Unattended Ubuntu Apt Installs

This will be short and sweet.

When you want to

apt-get install package

and you don't want to get prompted for values, etc, just add the following the front of your shell:

export DEBIAN_FRONTEND=noninteractive

Poof. Magic.

Sunday, January 2, 2011

Grepping out comments and blank lines

One thing we commonly see in open-source configs is an abundance of helpful comments. Often, they dwarf the number of active parameters being set and that can make things very hard to read.

In the blessed world of unix, there is an easy solution to this: you simply use grep to remove those lines from a listing of the contents of the file.

First, grep out the comments:

grep -v \# file

Usually, that will leave you with a lot of unnecessary whitespace, as well. So just pipe that output through an enhanced grep and filter for blank lines:

grep -v \# file | egrep -v "^$"

To clarify the above, here's a break down of each component:

grep - self explanatory
-v - tells grep to remove what matches rather than show it
\ - tells the shell the special character # is to be passed to the process without interpretation
# - the near-universal symbol for "comment" in config-ese
file - a stand in for whatever conf you're digging through
| - a pipe. Tells the shell to send the output of what's to the left of the pipe to what's on its right
egrep - grep with extended regular expressions (a sophisticated text manipulation language)
-v - telling egrep to remove the matches
"" - passing what's inside to grep without shell interpretation
^ - Means "the start of the line"
$ - Means "the end of the line"

So what you're telling it, in essence, is the following:

  1. Read the contents of file
  2. Remove all lines with "#"
  3. Pass the result to egrep
  4. Egrep: remove all lines which begin and immediately end (ie, a blank line) and output the result
This is one of those little tricks that goes a long way to simplifying interactions with Unix hosts and it's actually the first thing I do when I go to read any new service's default config.

Hope it helps.