1

I've used this answer, which only copies stdout to file:

$ cat /etc/dehydrated/syncNexusCertificatesHook.sh 
#!/bin/bash -ex

exec &> >(ts '[%Y-%m-%d %H:%M:%S]' | tee -a /var/log/dehydrated.log >&2 )
...

When running the script, there was a perl: warning: Setting locale failed. printed to the terminal, but not to the log file.

I want to have all output from the script printed with a timestamp printed to the console as well as saved to the log file. How can I achieve this?

mles
  • 4,534
  • 10
  • 54
  • 94

1 Answers1

1

To redirect both stdout and stderr of a command to a sequence of commands, and finally to both the console and a file, you can do it with this syntax

command |& ts '[%Y-%m-%d %H:%M:%S]' | tee file

which is the synonym of this

command 2>&1 | ts '[%Y-%m-%d %H:%M:%S]' | tee file

or redirecting both streams to a process substitution

command &> >(ts '[%Y-%m-%d %H:%M:%S]' | tee file)

or redirecting the output and then make the stderr a copy of stdout

command > >(ts '[%Y-%m-%d %H:%M:%S]' | tee file) 2>&1
thanasisp
  • 5,855
  • 3
  • 14
  • 31