Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 994
MacUsers Avatar asked Sep 02 '13 10:09

MacUsers


2 Answers

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.

like image 120
konsolebox Avatar answered Oct 23 '22 13:10

konsolebox


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.

like image 40
David Sainty Avatar answered Oct 23 '22 11:10

David Sainty