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?
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?
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
... | awk -f timetag.awk > ...
timetag.awk:
{
print "[" strftime() "] " $0
}
In shell: top | grep java | while read LINE ; do date "+%F %T ${LINE}" ; done
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