I have an external program which I can not edit. It writes its log to a file.

How do I add a timestamp to each line while the program writes to the logfile?
It does not write its output to stdout. The timestamp should at least include seconds. Preferably only using standard linux tools such as pipes, bash, cron etc.

After testing a few example scripts, I noticed that the program closes the logfile to reload its configuration, then it recreates the same file as a plain empty textfile. I guess this means one has to use another approach such as continously (cron) rechecking the file contents?

  • @Nils You linked to the question, not a specific answer. Furthermore all those examples only appear to be working with output on stdout, which I do not have. This is getting close (needs adding a timestamp). Please also note that I updated the question. – dirty bit Sep 30 '11 at 20:23
  • I was talking about the adddate() answer. But yes - that is taking stdout. Is there any special reason why the program can't use the syslog mechanism? – Nils Oct 1 '11 at 19:55

Can you create a fifo and configure your program to write its log in it?

If yes, create the fifo and write a simple shell script that reads from that and writes to log file pre-pending a time-stamp. Something like:


mkfifo $FIFOFILE
awk '{printf("%s - %s\n", systime(), $0);}' < $FIFOFILE > $LOGFILE

second version

If your program does log rotation and periodically deletes the fifo, you have to use another way.

You can use tail to monitor the program log file. Note that tail checks periodically the file so (if you are very unlucky) you could lose some log line.


    | awk '{printf("%s - %s\n", systime(), $0);}' \
  • I tried doing that but ran into a few problems; To test whether things work, I use tail -F foo.log (is that alright or does it cause more trouble?). The program sometimes reopens the logfile. It seems like the output from tail is buffered in some way. Furthermore the program seems to freeze when reopening the logfile (it does not react to input on stdin anymore). – dirty bit Sep 30 '11 at 19:00
  • In addition to my previous comment; See my revised question. The program recreates the file, which most likely causes the script as well as tail to fail, since there's no more fifo to read from. – dirty bit Sep 30 '11 at 20:28

Based on the syslog-idea in my comment:

tail -n1 -q -f --retry $YOURFILE 2>/dev/null |logger

This seems to work with CentOS 5. See you local man-pages for tail and logger.

In CentOS there is a warning about using "--retry" wich goes to stderr and has to be suppressed.

With logger you can send messages to syslog - which in turn will normally prepend a time stamp.

  • logger do not add time stamp. it is added by syslog after message is arrived. – Znik Mar 5 at 12:52
  • @Znik "which" references "syslog" so what? – Nils Mar 21 at 9:57

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.