Skip to content

Redirecting output to syslog

November 25, 2008

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
Advertisements

From → Solaris

2 Comments
  1. Anonymous permalink

    >who still writes bourne shell scripts
    me.
    #!/bin/sh
    (
    echo and this is how to do it portably
    ) 2>&1 | logger [options]
    exit 0

  2. UX-admin permalink

    "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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: