Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
    
    
@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
add comment

2 Answers

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:

#!/bin/sh
FIFOFILE=/tmp/program_log.fifo
LOGFILE=/var/log/program.log

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.

#!/bin/sh
PROGRAMLOGFILE=/tmp/program.log
MARKEDLOGFILE=/var/log/program.log

tail -F $PROGRAMLOGFILE \
    | awk '{printf("%s - %s\n", systime(), $0);}' \
    > $MARKEDLOGFILE
share|improve this answer
    
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
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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