1

I'm currently using top to gather the most resource intensive processes and sending them to influxdb. Currently, the command is like this:

top -bn1 -c -w250

Which basically runs top a single time (-bn1), with the full name of the command (-c) and without truncating (width 250). I run this every 15 seconds and process each output with a script.

However, I've noticed that running top itself like that is getting quite heavy. It's frequently the most intensive process. I suspect the reason for that is because since it gets terminated and restarts from zero every 15 seconds, it probably has to reload many things it only needs when starting up. That would explain why its CPU consumption drops considerably on the following iterations when executed during interactive mode.

So I tried to run it continuously in batch mode (removing -n1 argument) and storing the output in a file. But the file is getting huge, because it's not being overwritten at each iteration (instead, each output is being appended). Is it possible to store only the last iteration's output while not terminating the program?

My current attempt:

top -bc -d15 -w250 > /var/log/top.log

8
  • 1
    The reason it's often the top consumer is that it's by definition guaranteed to be running when it's outputting stuff to screen, and sleeping in between. This is true when running one iteration, or a hundred.
    – vidarlo
    Commented May 9 at 21:17
  • @vidarlo Fair enough, however I only notice this in the first iteration. Please try it yourself and see how the first measure is way higher than the others. I don't know if top really uses more resources when it's starting up, or if the first iteration is just not very accurate. But whatever the reason, it makes it difficult to track the top processes the way I am trying to do (in a non-interactive way).
    – Leandro 86
    Commented May 9 at 21:39
  • Why not use ps auxwww to dump all processes since you didn't state your purpose?
    – djdomi
    Commented May 10 at 5:41
  • 1
    top shows itself quite high in the top not only during start, I can state this as someone who had used it a lot during a career. Well, collecting statistics takes CPU time too, as well as presenting it to the user. What I don't understand is why you are sticking to top? Why not sar or atop which are designed to record the usage trace and then present the load state of a machine as historical information which you can walk to see "what happened" then. Why not filter ps (like, ps -eo %cpu,etimes,command | awk '$1 > 5.0 {OFS=","; print $1,$2,$3 }' (5.0 is a lowest percent to show)? Commented May 10 at 5:59
  • @Leandro86 I tested it.
    – vidarlo
    Commented May 10 at 6:45

2 Answers 2

0

Would you consider a dedicated data gatherer like Netdata for this?

That may be a cleaner method to obtain the data you're seeking.

enter image description here

1
  • I'm not sure, but I think I've experimented netdata before and I think it was too much for my needs (lots of sensors and info, which is great, but I'm trying some tailored/customized monitoring through Grafana). In the end, I managed to do it with atop, reading its output files through a script.
    – Leandro 86
    Commented 18 hours ago
0

As others suggested, running ‘ps’ may be better, but if you want to continuously write to file and have it rotated, see rotatelogs utility:

https://httpd.apache.org/docs/2.2/programs/rotatelogs.html

Essentially: top -b -c -w250 |rotatelogs ps.log 3600

Will rotate logs hourly. You can remove old logs using log rotate or any other script/utility you wish (find with ctime/mtime and -remove).

There are switches for predictable naming.

You could instead run with -bn1 and in your parser ignore “top” in that output.

New contributor
Robert Glonek is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .