Search code examples
linuxbashcron

How to add timstamps to crond's native logs?


I know this has been asked countless times but I am looking for a solution that uses crond's native log function. I do not want to pipe the output of each cron and prepend the timestamp.

I am launching crond like this:

crond -L /var/log/cron.log -f

the logs are like this:

crond: crond (busybox 1.30.1) started, log level 8
crond: USER root pid  16 cmd echo "hello"
crond: USER root pid  18 cmd echo "hello"
crond: USER root pid  19 cmd echo "hello"

I'd like to add the timestamp before the line. I do not want to add some stdout command to each individual cron and prepend the date.

Maybe I could watch the file and append to each new line or something? How do I get access to crond's stream and modify it?


Solution

  • I believe that the answer is that it's not possible to modify the crond output file.

    The actual implementation detail of the cron do not make it easy to control the log file for individual jobs. Also, the crond is running as root, which will make it hard to user jobs to change the file. Trying to change the file, while crond is running will likely result in problems.

    Consider instead the following option

    • Write a process that will tail -f the log file, and create a new log file, with each line prefixed by the timestamp.
    • Run the process at boot time.
    tail -f /var/log/cron.log | while read x ; do echo "$(date) $x" ; done >> /var/log/cron-ts.log
    

    Or configure to whatever format you need.