I have the following cron job running:

/home/ubuntu/scripts/austria/archive.sh >> /home/ubuntu/logs/austria/archive.log 2>> /home/ubuntu/logs/austria/archive.err

Every time the script runs, it makes a regular log file, and if there are errors, it updates the .err file with the errors. However, I was wondering if it would be possible to have the errors also include the date of the errors in the log. I'm very new to crontab, and I'm having trouble getting this to happen. Thanks!!


Pipe the output to ts(1) of moreutils among other such similar utilities, which prefix any input with a time stamp:

% (echo hi; sleep 3; echo there) | ts
Aug 28 18:52:42 hi
Aug 28 18:52:45 there

So assuming you want timestamps on just standard error:

...archive.sh 2>&1 >>.../out.log | ts >>.../err.log

Note that this is really tricky, and important that the shell redirections happen exactly as written. This is what I used to verify the shell redirections:

$ perl -le 'print "out"; warn "err!\n"' 2>&1 >> out | sed 's/^/blah /' > err
$ cat out
$ cat err
blah err!

Another option would be to pipe the output to logger, in which case syslog would prefix the logs with a timestamp and then dump the logs into some logfile, depending on the syslog daemon settings.

Yet another option might be to directly edit archive.sh to include appropriate date call output for everything sent to stderr:

echo `date` oh no >&2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.