Generating a file that is output from a command only if a condition is met

Asked

Viewed 192 times

0

Be the command:

top -o %MEM -b > file.txt

I would like you to file.txt only the values that are between two time periods, for example:

between 07 April 2017 07h and 07 April 2017 12h. Is it possible? This way I would have a file only with the entries that interest me.

I would like to store in an archive only entries that are between April 7, 2017 07h and April 7, 2017 12h!!!

  • I did not understand your question. The condition is related to some value issued by top?

  • Nao! would like to store in a file only the entries that are between April 7, 2017 07h and April 7, 2017 12h!!!

  • Where do you get this information from? You want to run the script only at these times according to the computer clock?

  • it would be a time that I would inform him. I could use Unix time to make it easier. Because top -the %MEM -b > file.txt generates a lot that I don’t care about. I just want the information between dates that I enter!

  • It is possible to do this?

  • You want to make a schedule, which has nothing to do with function parameters. I found this font here in English that can help you: https://www.howtogeek.com/101288/how-to-schedule-tasks-on-linux-an-introduction-to-crontab-files/

  • no! top -o %MEM -b > file.txt will generate a file with the use of memory and CPU with numerous days and times that do not interest me! i wanted to slice the file before it was generated to save only the above interval!

  • Then the exit from top do you have any relevant date information? If so, I would use a grep to select

  • I don’t understand! I could give an example?

  • The problem is that it takes a long time to generate the file. I would like to tell the top command to only store it in the file between the periods I reported

  • 1

    cat /var/log/apache.log /var/log/anaconda.log | grep jeff will only take the lines that have jeff. Read about grep and pipe

Show 6 more comments

2 answers

3

I imagine that the grep will not solve in this case, but you can with shell script pick line by line from the contents of the top, then go from Voce create a logic to pick the time and compare for example: Suppose the line comes like this: drwxrwxrwx Victor:Victor hh:mm:ss filename this is an example only you could take this line and break in the spaces, then take the 3 element that would be hh:mm:ss and then break in the ':' dai Oce would have separated hour, minute and second, after that Could you compare the time is equal or greater than 07? or is the time less than or equal to 11? and then put this line in the final file, the logic I’m sure works, but I’m rusty in shell script and will not be able to provide you the code :/

2


It’s not completely clear what you want...

We obviously can’t analyze the memory used in the past. Let’s start by storing memory usage (with top) from now until two hours from now. I would suggest that:

  • define a sampling interval (example 30 seconds)
  • define a number of samples (120 * 60s /30s = 240)
top -o '%MEM' -d 30 -n 240 -b > file.txt
... analisar_e_limpar file.txt > resumo.txt

(top has many options that allow you to hide, add fields, etc).

If we want to start logging somewhere in the future, we can use cron-tab or use the "wait ..." : sleep ...; top ...

  • how would it be with crontab? I wanted to monitor and save, next Monday, Tuesday and Wednesday from 07h to 15h

  • @Eds, look up the details on man 5 crontab but it would be something like editing the cron tab (crontab -e) and add to it a line like 0 7 * * mon,tue,wen /home/EdS/script (untested) where you chose the desired sampling and quantity

  • What’s the difference between doing > file.txt and >>file.txt ??

  • 1

    Using > file.txt Voce will overwrite whatever is in the file and place the new content, using >> file.txt Voce will concatenate the new content with what is already in the file

Browser other questions tagged

You are not signed in. Login or sign up in order to post.