prepend timestamp to continiously updating log file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting prepend timestamp to continiously updating log file
# 1  
Old 10-23-2012
prepend timestamp to continiously updating log file

Hi,

I have a process which outputs to a log.


Below is the code snippet:


Code:
 
process   &> $LOGFILE&



The log file keeps on updating whenever a transaction is processed.
The log file has a time stamp added so every time I kill the process and start the process a new log file is created with latest time stamp.

Because the log file would be growing forever, I would like to prepend time stamp for each line in the log file. I used the below way but instead of writing the log every time a transaction is processed, the time stamp is added to each line in the log after the kill the process.


Code:
 
process  | sed "s/^/$(date)/" &> $LOGFILE&



Can you let me know how do I prepend time stamp inside the log in real time?

Thank you!
# 2  
Old 10-23-2012
You don't... I can't think of an effective way to do this which doesn't involve fixing the application to do what you want in the first place.

If you must 'intercept' the log, make a new log, don't keep editing the old one in place. How many thousands of times will the application be killed to do it your way?

Code:
tail -f logfile | while read LINE
do
        echo $(date) "$LINE"
done > newlogfile

# 3  
Old 10-23-2012
@Corona688 - Your solution works if the log file which you took as input is already created. But in this case, the log file will be updating always whenever a transaction is processed.

Also, per your suggestion, I need to create another log file which I do not want to.

I want to create only once when the process runs and output the detail of process to log with time stamp before each line.

FYI, the command is inside a shell script and all this is desired when the script is executed.

And, I will be killing the process may be once in a day.

Last edited by rajkumarme_1; 10-23-2012 at 02:55 PM.. Reason: need to add a comment
# 4  
Old 10-23-2012
I badly misunderstood what you were saying, my apologies.

My suggestion would still function with a little modification however.

Code:
process | while read LINE
do
        echo "$(date) $LINE"
done > outputfile

Anything involving pipes may buffer unexpectedly, however; collecting lines until the buffer fills and you get a bunch of lines at once. Behavior may depend on your system, try it and see.
# 5  
Old 10-31-2012
Thanks Corona688

It worked. I'll do some tests and see if there are any issues with pipes.
This User Gave Thanks to rajkumarme_1 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Log file is not updating when I run shell scripts scheduled thru crontab

Hi Forum, Good Day! I have created an empty html file wtih permissoin 777 created shell script(with permission 777) , code is below. #=======================start============== . /data09/oracle/apps_st/appl/D_oraapp095.env rm -rf /home/mnp/Test_log.txt echo... (1 Reply)
Discussion started by: kartheekbk
1 Replies

2. Shell Programming and Scripting

Need to get all the records from a log file greater than timestamp supplied.

I have a log file which has records with hung thread information/error I need to find out hung thread from log file greater than timestamp supplied. 00000026 ThreadMonitor W WSVR0605W: Thread "WebContainer : 1" (00000027) has been active for 701879 milliseconds and may be hung. There is/are... (6 Replies)
Discussion started by: megh
6 Replies

3. Shell Programming and Scripting

Prepend TimeStamp to STDERR & STDOUT to a file

Currently I am redirecting STDERR and STDOUT to a log file by doing the following { My KSH script contents } 2>&1 | $DEBUGLOG Problem is the STDERR & STDOUT do not have any date/time associated. I want this to be something that i can embed into a script opposed to an argument I use... (4 Replies)
Discussion started by: nitrobass24
4 Replies

4. Shell Programming and Scripting

Continiously monitor the log file

Hi Friends, I am trying to write a script which continiously monitor one specific error message from a log file. This script should continiously monitor the file for the error and send out the email when detect the error message. I tried the below command but fails. Please help me. tail -f... (4 Replies)
Discussion started by: arumon
4 Replies

5. Shell Programming and Scripting

Delete log file entries based on the Date/Timestamp within log file

If a log file is in the following format 28-Jul-10 ::: Log message 28-Jul-10 ::: Log message 29-Jul-10 ::: Log message 30-Jul-10 ::: Log message 31-Jul-10 ::: Log message 31-Jul-10 ::: Log message 1-Aug-10 ::: Log message 1-Aug-10 ::: Log message 2-Aug-10 ::: Log message 2-Aug-10 :::... (3 Replies)
Discussion started by: vikram3.r
3 Replies

6. Shell Programming and Scripting

How to search backwards in a log file by timestamp of entries?

Hello. I'm not nearly good enough with awk/perl to create the logfile scraping script that my boss is insisting we need immediately. Here is a brief 3-line excerpt from the access.log file in question (actual URL domain changed to 'aaa.com'): 209.253.130.36 - - "GET... (2 Replies)
Discussion started by: kevinmccallum
2 Replies

7. Shell Programming and Scripting

concatenate log file lines up to timestamp

Hi, Using sed awk or perl I am trying to do something similar to https://www.unix.com/shell-programming-scripting/105887-sed-awk-concatenate-lines-until-blank-line-2.html but my requirement is slightly different. What I am trying to accomplish is to reformat a logfile such that all lines... (4 Replies)
Discussion started by: AlanC
4 Replies

8. UNIX for Dummies Questions & Answers

prepend columns to a file

I have two files. File 'a' has contents: 1|1 2|2 3|3 4|4 and file 'b' has contents: abc|def hij|klm nop|qrs tuv|wxy I would like to prepend file 'a' to file 'b' (with pipe) such that the contents of 'b' will be: 1|1|abc|def 2|2|hij|klm 3|3|nop|qrs 4|4|tuv|wxy (3 Replies)
Discussion started by: ChicagoBlues
3 Replies

9. Shell Programming and Scripting

awk updating one file with another, comparing, updating

Hello, I read and search through this wonderful forum and tried different approaches but it seems I lack some knowledge and neurones ^^ Here is what I'm trying to achieve : file1: test filea 3495; test fileb 4578; test filec 7689; test filey 9978; test filez 12300; file2: test filea... (11 Replies)
Discussion started by: mecano
11 Replies

10. UNIX for Dummies Questions & Answers

Spooling a log file with timestamp

Hi From shell script i am invoking sqlplus to connect to oracle database and then i spool a csv file as with output. What i want to do is to change the file name with timestamp on it so after spooling finish shell script change file name with time stamp. can someone help me to do that . Thanks... (2 Replies)
Discussion started by: ukadmin
2 Replies
Login or Register to Ask a Question