2

I have a crontab running like:

*/15 * * * 4,5   /apps/ins/sid/compare_stats 2>> /apps/ins/sid/compare_stats.err

Everything working as expected. the only thing is I want my error logs to generate in the compare_stats.err file like this:

Jul 3 14:45:04 <error text>

which means I just want to add a date along with this. Is there any way to do it by modifying the crontab entry ( without really making any change in my script) ?

Thanks in advance.

dig_123
  • 2,240
  • 6
  • 35
  • 59
  • 1
    possible duplicate of [Is there a Unix utility to prepend timestamps to lines of text?](http://stackoverflow.com/questions/21564/is-there-a-unix-utility-to-prepend-timestamps-to-lines-of-text) – slhsen Jul 03 '14 at 07:52
  • @slhsen I already tried something like this : `*/15 * * * 4,5 /apps/ins/sid/compare_stats|/usr/bin/awk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0; fflush(); }' 2>> /apps/ins/sid/compare_stats.err` doesn't really work for me – dig_123 Jul 03 '14 at 08:05
  • I also don't have any of the default utilities as mentioned in http://stackoverflow.com/questions/21564/is-there-a-unix-utility-to-prepend-timestamps-to-lines-of-text as I'm using a `customized` `RHEL 5.10` – dig_123 Jul 03 '14 at 08:06

1 Answers1

1

Use the ts command which is part of the moreutils package. E.g.:

*/15 * * * 4,5   /apps/ins/sid/compare_stats | ts '[%Y-%m-%d %H:%M:%S]' 2>> /apps/ins/sid/compare_stats.err

This will prepend the timestamp to every line of the output and save it into your log.

Simon Woodside
  • 7,175
  • 5
  • 50
  • 66