Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i add a timestamp to my script using the linux free command (free | grep mem | awk '{print $4/$2 * 100.0}')

I just want to add a timestamp to a script output file, and also continue to add output to the same file.

This is the script im using and want to add a timestamp in the output file and want to be able to add entries to the same file over and over again. I would also need to see the timestamp for each entry.

free | grep mem | awk '{print $4/$2 * 100.0}' > free.txt

free | grep mem | awk '{print $4/$2 * 100.0}' > free.txt

The results of the script currently is that i can save the output of a single query but it doesnt have a timestamp, and I dont know how to make it recurring output to the same file for a specific amount of time. Ideally i would make the script run in 5 minute intervals for a week, and then start a new file. I'm not sure if this is even possible. The end result would show a list of free memory percentages over the entire week, so I could see the trends in memory usage and monitor for oversubscription of memory.

like image 349
mbhoch Avatar asked Dec 02 '25 13:12

mbhoch


1 Answers

(date "+%c" | tr -d '\n'; free | awk '/^Mem:/ {print " ", ($4/$2 * 100.0}') > free.txt

Or whatever. I hate tr, but it's the fatest way to strip newlinws. Other time formats can be found in date's manpages (not Mac, of course). In that case, google google.

Also you don't need grep here, and you're performing a case-sensitive search anyway, which is bad because the output of free usually creates /^Mem:.*$/

like image 176
gregmark Avatar answered Dec 04 '25 05:12

gregmark



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!