-2

I have this command:

top -n 14400 -a -b -d 1 > /toplogfile.txt

I want to make a script which is run by cron every day to capture top output, and if it is possible gzip the output file after the script finished.

How can I implement this?

  • you would need another cron job if you want to gzip things after this first script finishes. Why not do it in the same script? – Timo Jan 24 '14 at 13:40
0

top -n 1 will run top for 1 iteration. You can direct that output to a file and compress it using a simple shell script like this: top -b -n 1 | gzip -c > top_output.gz

top -b specifies that you want top to run in "Batch Mode" which eliminates all of the control characters that top normally uses to format the output.

-n 1 tell top to run 1 iteration

gzip -c tells gzip to compress the data it is being sent

  • I am quite new in linux, I do not know how to write this script at all. Do I have to put this command into an file.sh and set a cron job to execute this file in a specific time? – zsola3075457 Jan 24 '14 at 14:32
  • I must have missed the command you put in the OP, but the -a option isn't valid on the version of top I use. You could put this command directly into cron, but I would say that is bad practice, if this is something you want to run 14400 iterations of. It seems like you are trying to gather system statistics and I am sure there would be a better way of doing so than using top. – David Wilkins Jan 24 '14 at 14:37
  • -a is tested on my configuration, it sorts the process list by memory usage – zsola3075457 Jan 24 '14 at 14:39
  • @Davis Wilkins you are right. Sometimes one of the processes use up almost 50% of my memory, but it runs only for a short time. This drops out the majority of usefull cache. I want to catch this process. You have better way to do this? – zsola3075457 Jan 24 '14 at 14:43
  • ps aux --sort -rss will give you a list of the running processes sorted by memory usage, it's much more efficient than running top continuously. If you really needed to you could run this once a second and gzip the output as above to try to catch the culprit – David Wilkins Jan 24 '14 at 14:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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