Reputation: 2229
I'm redirecting only the STDERR to a file (which is more or less working) but I also want to add/prepend a time-stamp for each redirection. I don't want the time in every line, just at the beginning of the block. I tried this:
UKMAC08:~ san$ ( date 1>&2; df -jj ) 2>&1 1>/dev/null 2>testErr.log; cat testErr.log
Fri 1 Aug 2014 10:50:00 BST
df: illegal option -- j
usage: df [-b | -H | -h | -k | -m | -g | -P] [-ailn] [-T type] [-t] [filesystem ...]
Which is doing what I want to do for an unsuccessful run but also putting the time-stamp when it's a success:
UKMAC08:~ san$ ( date 1>&2; df -h ) 2>&1 1>/dev/null 2>testErr.log; cat testErr.log
Fri 1 Aug 2014 10:50:07 BST
How can I avoid that. Best!
Upvotes: 3
Views: 1489
Reputation: 62389
How about something like this:
date > stderr.log
command 2>> stderr.log
That seems the simplest way to me - simply pre-seed your output file with the date, then append the stderr output, instead of overwriting the file...
Upvotes: 0
Reputation: 75488
This can do:
OUT=$(df -h 2>&1 >/dev/null) || { date; echo "$OUT"; } >testErr.log
Test:
$ : > testErr.log; OUT=$(df -jj 2>&1 >/dev/null) || { date -u; echo "$OUT"; } >testErr.log; cat testErr.log
Fri Aug 1 12:10:29 UTC 2014
df: invalid option -- 'j'
Try 'df --help' for more information.
$ : > testErr.log; OUT=$(df -h 2>&1 >/dev/null) || { date -u; echo "$OUT"; } >testErr.log; cat testErr.log
(no output)
You can change >testErr.log
to >>testErr.log
to append.
And this can be more efficient:
OUT=$(exec df -h 2>&1 >/dev/null) || { date; echo "$OUT"; } >testErr.log
Upvotes: 2