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
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 guest on November 25, 2008 at 11:16 AM 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 25, 2008 at 11:29 PM GMT #

Post a Comment:
Comments are closed for this entry.
About

This is the old blog of Chris Gerhard. It has mostly moved to http://chrisgerhard.wordpress.com

Search

Archives
« April 2014
MonTueWedThuFriSatSun
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    
       
Today