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