Michael Hoeller
Michael Hoeller

Reputation: 24328

How to send script output to syslog

I want to change a script which currently writes its output to logfile.

 ~/bin/setBatteryProtect.sh >> /tmp/battery.log

I want to have the output send to syslog. What is the best option to do so. The most reliable version seems to be to use logger. I actually would like to avoid to change the script - so I am looking for an option to send the complete output to syslog. Is this possible?

Upvotes: 2

Views: 8207

Answers (2)

jeseric
jeseric

Reputation: 1

put this at the top of your script? I found it in another site

exec 1> >(logger -s -t $(basename $0)) 2>&1

source: "https://www.urbanautomaton.com/blog/2014/09/09/redirecting-bash-script-output-to-syslog/"

Upvotes: 0

Kusalananda
Kusalananda

Reputation: 15633

logger reads from standard input if no message is specified on the command line and if -f isn't used (reads from file).

utility | logger

would log to syslog using logger, with one log entry per line of output from utility.

Add -t tag to add a tag to the log. Without it, the username of the user executing logger will be used as the tag.

Upvotes: 4

Related Questions