Printing message on the screen and also send to the syslog at the same time

MacUsers picture MacUsers · Sep 2, 2013 · Viewed 7.8k times · Source

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!!

Answer

konsolebox picture konsolebox · Sep 2, 2013

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.