The dot in ... --- ...

Chris Gerhard's Weblog

« Two pools on one... | Main | Adding dependancies... »

20081125 Tuesday November 25, 2008

Redirecting output to syslog

People are always asking this and often when they are not they should be. How do you redirect all the output from a script to syslog?

The obvious is:

my_script | logger -p local6.debug -t my_script 2>&1

but how can you do that from within the script? Simple put this at the top of your script:


#!/bin/ksh

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

exec >&p 2>&1


Clearly this is korn shell specific but then who still writes bourne shell scripts. If you script was called redirect you get messages logged thus:

Nov 25 17:40:41 enoexec redirect[17449]: [ID 702911 daemon.notice] bar

( Nov 25 2008, 05:44:27 PM GMT ) Permalink Trackback

   
Comments:

>who still writes bourne shell scripts

me.

#!/bin/sh
(
echo and this is how to do it portably
) 2>&1 | logger [options]
exit 0

Posted by 192.18.1.36 on November 25, 2008 at 07:16 PM GMT #

"Clearly this is korn shell specific but then who still writes bourne shell scripts."

I do. And in /sbin/sh, no less!

No time to screw around in Korn shell (I used to, but not anymore!), life is too short! I use the Bourne shell as logic control/shim, and do the heavy lifting work in AWK.

Posted by UX-admin on November 26, 2008 at 07:29 AM GMT #

Post a Comment:

Comments are closed for this entry.

Valid HTML! Valid CSS!

Except where otherwise noted, this site is
licensed under a Creative Commons License 2.0

This is a personal weblog, I do not speak for my employer.