Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Say I want to monitor memory usage on a java process overnight.

I can do something like

top | grep java > out.log

I will get a log file with a whole bunch of lines.

Is there an easy way to get a data/time inserted in front of each line?

share|improve this question

4 Answers 4

... | awk -f timetag.awk > ...

timetag.awk:

{
  print "[" strftime() "] " $0
}
share|improve this answer
1  
For versions of AWK that don't have strftime: ... | awk '{"date" | getline d; close("date"); print "[" d "] " $0}' which also illustrates doing it as a one-liner (it could be put in a script file as in the answer above). –  Dennis Williamson Nov 3 '10 at 19:55
    
Your one liner works fine on Linux. On Solaris it gives me this error:awk '{"date" | getline d; close("date"); print "[" d "] " $0}' awk: syntax error near line 1 awk: illegal statement near line 1 –  James Dean Nov 4 '10 at 17:40

Don't reinvent the wheel. Write the messages to syslog using logger(1), which is supported on just about every Unix flavor. Syslog will take care of the timestamps for you. Your log data will be stored to a system log like /var/log/messages (This is configurable). You don't need to worry about cleaning up the logfiles later on (especially if this job runs forever), because the system logs are automatically rotated by logrotate/newsyslog.

top | grep java | logger -t java_cpuhog

Or, if you really want to write it to your own file. But then you need to clean up the file afterwards:

top | grep java | logger -t java_cpuhog -f /var/log/java-top.log
share|improve this answer
    
This. logger must be one of the most under-valued pieces of software there are. Some wrapper should yell "USE logger instead, YOU FOOL!" at you every time it detects pattern like date >>/var/log/myapp.log && echo "I'm just logging here." >>/var/log/myapp.log –  Janne Pikkarainen Nov 5 '10 at 7:51
    
I think you need to be root for this (which I am not :-( ) –  James Dean Nov 5 '10 at 13:44
    
@James : No, regular users can use logger to write to the system logs. Remember about all those other unix programs which run as other users-- they still log to syslog. Many of them don't run as root. –  Stefan Lasiewski Nov 5 '10 at 21:13
    
@James : Although, you might need to be root to read from some of the system logs. However I believe that, /var/log/messages should be readable by most users on a system. To test this, do echo jamestest | logger, and then look or the output in /var/log/messages. –  Stefan Lasiewski Nov 5 '10 at 21:15
    
Also note that you can still use the second option above. echo stefantest | logger -f ~/stefantest.log –  Stefan Lasiewski Nov 5 '10 at 21:37

In shell: top | grep java | while read LINE ; do date "+%F %T ${LINE}" ; done

share|improve this answer
1  
%D and %F are redundant. Perhaps you meant %F and %T? –  Dennis Williamson Nov 3 '10 at 23:52
    
Thanks, edited to change as suggested. –  Slartibartfast Nov 5 '10 at 3:03

You could create something like stamp containing

#!/usr/bin/perl -p
s/^/localtime()." "/e;

Then run

top -b | grep java | stamp > out.log

Don't forget to use top -b if you are not using it interactively. Of course, top already puts a timestamp each time it runs. If you don't necessarily need the date on each line, just use top on its own since it puts a timestamp on each iteration. I sometimes use this to get 10 second samples for a full day to review later:

top -b -d 10 -c -n 8640
share|improve this answer
    
Better Perl would be to not even have a regex. $_ = localtime() . ' ' . $_. –  Andy Lester Nov 3 '10 at 20:16

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.