In Linux I'm starting a program called $cmd
in an init script (SysVInit). I'm already redirecting stdout and stderr of $cmd
into two different logfiles called $stdout_log
and $stderr_log
. Now I also want to add a timestamp in front of every line printed into the logfiles.
I tried to write a function called log_pipe as follows:
log_pipe() {
while read line; do
echo [$(date +%Y-%m-%d\ %H:%M:%S)] "$line"
done
}
then pipe the output of my script into this function and after that redirect them to the logfiles as follows:
$cmd | log_pipe >> "$stdout_log" 2>> "$stderr_log" &
What I get is an empty $stdout.log
(stdout) what should be okay, because the $cmd
normally doesn't print anything. And a $stderr.log
file with only timestamps but without error texts.
Where is my faulty reasoning?
PS: Because the problem exists within an init script I only want to use basic shell commands and no extra packages.