The other answers mostly work, but have some drawbacks. In particular:
- Many require installing a command not commonly found on linux systems, which may not be possible or convenient.
- Since they use pipes, they don't put timestamps on stderr, and lose the exit status.
- If you use multiple pipes for stderr and stdout, then some do not have atomic printing, leading to intermingled lines of output like
[timestamp] [timestamp] stdout line \nstderr line
- Buffering can cause problems, and
unbuffer
requires an extra dependency.
To solve (4), we can use stdbuf -i0 -o0 -e0
which is generally available on most linux systems (see How to make output of any shell command unbuffered?).
To solve (3), you just need to be careful to print the entire line at a time.
- Bad:
ruby -pe 'print Time.now.strftime(\"[%Y-%m-%d %H:%M:%S] \")'
(Prints the timestamp, then prints the contents of $_
.)
- Good:
ruby -pe '\$_ = Time.now.strftime(\"[%Y-%m-%d %H:%M:%S] \") + \$_'
(Alters $_
, then prints it.)
To solve (2), we need to use multiple pipes and save the exit status:
alias tslines-pipe="stdbuf -i0 -o0 ruby -pe '\$_ = Time.now.strftime(\"[%Y-%m-%d %H:%M:%S] \") + \$_'"
function tslines() (
stdbuf -o0 -e0 "$@" 2> >(tslines-pipe) > >(tslines-pipe)
status="$?"
exit $status
)
Then you can run a command with tslines some command --options
.
This almost works, except sometimes one of the pipes takes slightly longer to exit and the tslines
function has exited, so the next prompt has printed. For example, this command seems to print all the output after the prompt for the next line has appeared, which can be a bit confusing:
tslines bash -c '(for (( i=1; i<=20; i++ )); do echo stderr 1>&2; echo stdout; done)'
There needs to be some coordination method between the two pipe processes and the tslines function. There are presumably many ways to do this. One way I found is to have the pipes send some lines to a pipe that the main function can listen to, and only exit after it's received data from both pipe handlers. Putting that together:
alias tslines-pipe="stdbuf -i0 -o0 ruby -pe '\$_ = Time.now.strftime(\"[%Y-%m-%d %H:%M:%S] \") + \$_'"
function tslines() (
# Pick a random name for the pipe to prevent collisions.
pipe="/tmp/pipe-$RANDOM"
# Ensure the pipe gets deleted when the method exits.
trap "rm -f $pipe" EXIT
# Create the pipe. See https://www.linuxjournal.com/content/using-named-pipes-fifos-bash
mkfifo "$pipe"
# echo will block until the pipe is read.
stdbuf -o0 -e0 "$@" 2> >(tslines-pipe; echo "done" >> $pipe) > >(tslines-pipe; echo "done" >> $pipe)
status="$?"
# Wait until we've received data from both pipe commands before exiting.
linecount=0
while [[ $linecount -lt 2 ]]; do
read line
if [[ "$line" == "done" ]]; then
((linecount++))
fi
done < "$pipe"
exit $status
)
That synchronization mechanism feels a bit convoluted; hopefully there's a simpler way to do it.
cat somefile.txt
a bit "misleading"? I'd expect it to happen "at once" and have a single timestamp. Wouldn't this be a better test program:(echo a; sleep 1; echo b; sleep 3; echo c; sleep 2)
?