Friday, January 29, 2010

Redirecting output to syslog

I don't know why I never looked into this before, but oh well. If you have a script and you would really like to have the output go to syslog rather than redirecting to a file that will get huge, you can do this:

2>&1 | logger -p daemon.notice -t ${0##*/}[$$]

e.g.

#!/bin/bash
/usr/local/bin/something 2>&1 | logger -p daemon.notice -t ${0##*/}[$$]

This will result in stderr and stdout going to /var/log/messages on standard linux distro like Centos.

Very handy indeed!