Only pipe STDERR output in bash
with timestamp
bash
In order to discard standard output and only log the standard error, following command can be used. The second part of command is to prefix the current timestamp in the output
sh monitor 2>&1>/dev/null | ts '[%Y-%m-%d %H:%M:%S]'
dash
If need to run in dash
, such as running in crontab
, above syntax is wrong, use following command instead
sh monitor 3>&1 1>/dev/null 2>&3 3>&- | ts '[%Y-%m-%d %H:%M:%S]'
Note: This command can be run in bash
too
References
Prepending a timestamp to each line of output from a command
Pipe only STDERR through a filter