1

I have tried to use these commands to test my shell script:

logger "hello i am fun" | tee -a test 

But the test file still empty. I have also tried to use echo

echo "hello i am fun" |tee -a test >logger

It also failed writing to test file. how can I use the logger with tee -a?

runandfun
  • 11
  • 4

1 Answers1

0

logger has an -s flag:

-s Output the message to standard error as well as to the system log.

Source: LOGGER(1) Man Pages

You can use the -s to log the message to standard error. Then redirect stderr to stdout and then stdout to /dev/null. Pipe the whole thing into tee, and you'll end up with your message in your syslog as well as the path you specified in tee.

Example:

joeyoung$ logger -s -p local0.notice -t TEST "test message" 2>&1 >/dev/null |  tee -a /tmp/teetemp.log
Oct  3 11:11:54 localhost TEST[4231] <Notice>: test message
joeyoung$ tail -n1 /var/log/messages
Oct  3 11:11:54 localhost TEST[4231]: test message
joeyoung$ cat /tmp/teetemp.log
Oct  3 11:11:54 localhost TEST[4231] <Notice>: test message

For proper attribution, some inspiration taken from: https://stackoverflow.com/a/2342841/2744166

Community
  • 1
  • 1
Joe Young
  • 5,749
  • 3
  • 28
  • 27