Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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.

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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top