Tuesday 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
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.
| « November 2009 | ||||||
| Mon | Tue | Wed | Thu | Fri | Sat | Sun |
|---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | |
9 | 10 | 11 | 13 | 14 | 15 | |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | ||||||
| Today | ||||||
>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 #