3

Tip #311   Filter stderr for cron jobs

Tired to get your mailbox full of cron information messages?
Eg.: some programs (pg_dump, /etc/init.d/xxx, ...) output informational messages on stderr, but you can't close stderr because it may also contain failure informations.

#!/bin/bash

LOG="/tmp/myscript-`date +%s`.tmp" IGNORE='^(Username: Password:|)[[:space:]]*$' # message to ignore, for demonstration only exec 3<&2; exec 2>$LOG # duplicate stderr for later use; redirect stderr to $LOG file
# insert your code (eg.: echo -e "login\npassword"|pg_dump -u user > file.dump)
exec 2<&3 # restore stderr # if $LOG file is not empty and contain anything else than what should be ignored, output it to stderr (for demonstration purpose) [ -s $LOG ] && egrep -vq $IGNORE $LOG && egrep -v $IGNORE $LOG >&2 rm -f $LOG