2

This question already has an answer here:

I'm extracting some performance metrics from "top" and saving them into file

top -b | grep 'tesseract\|node\|java\|beam.smp\|dockerd' > testm.txt

so for every second I'm getting next output:

   535 rabbitmq  20   0 1246756 128432   5536 S   1.3  3.4 618:33.41 beam.smp
   589 root      20   0  351040  47836  25740 S   0.0  1.3  12:38.72 dockerd
  1980 root      20   0 2236796  36844  15980 S   0.0  1.0   6:11.59 java
  1995 root      20   0 1766008 241428  21844 S   0.0  6.4  11:26.85 java
 29965 root      20   0 1107460  63732  19328 S   0.0  1.7   0:01.69 node

I'm curious how to add timestamps for every chunk of this output, because when running this script for a long period of time it become impossible to find out when some random data chunk had been collected.

Thanks in advance!

marked as duplicate by muru, thrig, Satō Katsura, xhienne, Thomas Nyman Mar 20 '17 at 20:44

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    Use ts: top -b | grep 'tesseract\|node\|java\|beam.smp\|dockerd | ts ... – muru Mar 20 '17 at 12:44
  • 1
    top already has a timestamp'ish on the topmost line: top - 20:11:51 up 10 days, ... FWIW – rogerdpack Dec 21 '18 at 20:13
6

Adding a timestamp to each line of top output for an individual run of top -b can be easily done using awk instead of grep for your filtering, because awk has time functions built in, and can inject them into it's output.

I'm not running most of these programs, so I've added bash and ssh to the filter in my examples...

You can do it with a simple epoch timestamp (seconds since 01-JAN-1970):

top -b | awk '/bash|ssh|tesseract|node|java|beam.smp|dockerd/ {print systime(), $0}'

Which is smaller to store, and cleaner to look at:

bash-$ top -b | awk '/bash|ssh|tesseract|node|java|beam.smp|dockerd/ {print systime(), $0}'
1490018813  1229 root      20   0  891536  42868  28348 S   0.0  0.3  15:07.30 dockerd
1490018813  1240 root      20   0   65520   6208   5488 S   0.0  0.0   0:00.01 sshd
1490018813  2666 tim       20   0   24336   7136   3492 S   0.0  0.0   0:00.05 bash
1490018813  2710 tim       20   0   46984   5324   4644 S   0.0  0.0   0:09.08 ssh
1490018813  2783 root      20   0  105988   7184   6184 S   0.0  0.0   0:00.00 sshd
1490018813  2804 root      20   0  105988   7404   6408 S   0.0  0.0   0:00.00 sshd

Or with a more human readable timestamp.

top -b | awk '/bash|ssh|tesseract|node|java|beam.smp|dockerd/ {print strftime("%Y-%m-%d-%H:%M:%S", systime(), $0}'

Which is faster to understand:

bash-[541]$ top -b -n 3 | awk '/ssh|bash|java|dockerd/ {print strftime("%Y-%m-%d-%H:%M:%S", systime()), $0}'
2017-03-20-10:04:23  1229 root      20   0  891536  42868  28348 S   0.0  0.3  15:07.24 dockerd
2017-03-20-10:04:23  1240 root      20   0   65520   6208   5488 S   0.0  0.0   0:00.01 sshd
2017-03-20-10:04:23  2666 tim       20   0   24336   7136   3492 S   0.0  0.0   0:00.05 bash
2017-03-20-10:04:23  2710 tim       20   0   46984   5324   4644 S   0.0  0.0   0:09.08 ssh
2017-03-20-10:04:23  2783 root      20   0  105988   7184   6184 S   0.0  0.0   0:00.00 sshd
2017-03-20-10:04:23  2804 root      20   0  105988   7404   6408 S   0.0  0.0   0:00.00 sshd
2017-03-20-10:04:23  2909 tim       20   0  105988   5072   4072 S   0.0  0.0   0:00.20 sshd
2017-03-20-10:04:23  2931 tim       20   0  105988   4252   3224 S   0.0  0.0   0:00.00 sshd

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