7

I'm trying to implement -s (i.e. silent) option in my script - when given the Errors/Info etc, will be send to the syslog otherwise printing on the screen and also sending to the syslog at the same time. That's what I'm doing:

echo -e "This Is a Test Message\nWell, not really!!"  2>&1 | logger

to send the echo message to the syslog (which doesn't print on-screen) but couldn't just figure out how to do the both at the same time. I see people only talk about either logging with syslog or sending log to a different file whilst printing on the screen but not the situation that I'm trying deal with. Any help or pointer would be greatly appreciated. Cheers!!

MacUsers
  • 2,091
  • 3
  • 35
  • 56
  • you can pipe to `tee` : `... | tee -a /var/log/syslog` – hek2mgl Sep 02 '13 at 10:52
  • 2
    Don't use `tee -a /var/log/syslog` - only root can do that, and syslogd might do all kinds of alternative handling other than just appending to that file. – David Sainty Sep 02 '13 at 10:57
  • @hek2mgl: True, but that file varies from system to system; e.g. `/var/log/messages` for Red Hat based system. Using `logger` is safer IMO. Cheers!! – MacUsers Sep 02 '13 at 10:58

3 Answers3

4

If you want to send the message to syslog and to stdout (not stderr), you can do this too:

echo -e "This Is a Test Message\nWell, not really!!" | tee >(exec logger)

And the efficient way to do it (if you're creating a function):

exec 40> >(exec logger)

function log {
    echo -e "$1"
    echo -e "$1" >&40
}

log "something..."

exec 40>&-  ## optionally close it at the end of the script.

That would save your script from calling external binary logger everytime you do an echo.

konsolebox
  • 72,135
  • 12
  • 99
  • 105
  • How do you call the function? – Juto Sep 02 '13 at 13:54
  • @konsolebox: I think this is the way to go for me. So, just to mack sure, if I use custom file descriptor, there is no way to do it other than using the `echo` twice? cheers!! – MacUsers Sep 02 '13 at 14:18
  • @Juto: You call the function just the way konsolebox did: `log "Print something..."`. *log* is the name of the function. – MacUsers Sep 02 '13 at 14:34
  • You could also have tee: `exec 40> >(exec tee >(exec logger))`, though I'd still recommend just having one process for it. It's still the same anyway. Output is duplicated in tee. – konsolebox Sep 02 '13 at 14:35
  • But of course you're using another function to send output to `&40` and you only want to do it once, then the new method could apply. – konsolebox Sep 02 '13 at 14:39
1

In that case, just echo the same message twice. You can do:

echo -e "This Is a Test Message\nWell, not really!!" | tee /dev/stderr | logger

But, it's actually simpler and more efficient to do:

error='This Is a Test Message\nWell, not really!!'
echo -e "$error" >&2
echo -e "$error" | logger

The >&2 sends the output to stderr, rather than stdout.

David Sainty
  • 1,398
  • 12
  • 10
0

You can do:

echo -e "This Is a Test Message\nWell, not really!!" | logger -s

The manual page (man logger) states:

-s     Log the message to standard error, as well as the system log.
David Sainty
  • 1,398
  • 12
  • 10
  • Already tried `-s` but didn't like the fact that it prefixes the message with username: `admin: This Is a Test..."`. It also can't handle `\n` for the new line, which is not good for me. Cheers!! – MacUsers Sep 02 '13 at 11:01
  • I removed 2>&1 because it's essentially superfluous - it's not expected to have any effect here regardless of whether the -s option is used or not. – David Sainty Sep 02 '13 at 11:02