#!/usr/bin/env bash set -eu -o pipefail SECONDS=${1:-1000000} # Initial script output several parameters, as both numbers and percentages. Now only outputting % values in effort to: # a) make output easier to read in all one format # b) lack of variables to compute may speed things up # Also using the WIDE format to split out buffer and cache to make things easiest to follow. Original code removed from file and just script available in "Old" # Further updates using the UNIX help "https://community.unix.com/t/if-memory-is-less-than-x-do-this-or-greater-than-x-do-another/386675" # PTS, 2022-05-24 # Total memory available: RAM_Total_Num() { free -h | tail -n 2 | head -n 1 | cut -b 15-18 } SWAP_Total_Num() { free -h | tail -n 1 | cut -b 15-18 # Swap is HDD memory used as RAM. Used SWAP should be as low as possible. } SD_Total_Num(){ df -k | grep root | awk '{printf ("%4.3f"), $2 / (1024*1024)}' # Using same format as the available memories rather than the initial code of using head and tail, and cutting numbers. This also returns as numerical so can do formatting with it, so here we have value in GB. } # Here's the change in this script - just output memories as percentages of the TOTAL. The order below is the order in which they occur in the terminal display. RAM_Used_Perc() { free -w | grep Mem | awk '{printf ("%5.1f"), $3/$2 * 100.0}' # https://stackoverflow.com/questions/10585978/how-to-get-the-percentage-of-memory-free-with-a-linux-command } RAM_Free_Perc() { free -w | grep Mem | awk '{printf ("%5.1f"), $4/$2 * 100.0}' } RAM_Shared_Perc(){ free -w | grep Mem | awk '{printf ("%5.1f"), $5/$2 * 100.0}' } RAM_Buf_Perc(){ free -w | grep Mem | awk '{printf ("%5.1f"), $6/$2 * 100.0}' } RAM_Cash_Perc(){ free -w | grep Mem | awk '{printf ("%5.1f"), $7/$2 * 100.0}' } RAM_Available_Perc() { free -w | grep Mem | awk '{printf ("%5.1f"), $8/$2 * 100.0}' } # Swap memory should not increase SWAP_Used_Perc() { free -w | grep Swap | awk '{printf ("%5.1f"), $3/$2 * 100.0}' } # And just for the sake, output the total available on the micro SD (along with location). Initially output in kB, but just output as percentage as "df -k" reports this anyway! SD_Location() { df -k | head -n 2 | tail -n 1 | cut -b 1-9 } SD_Available_Perc(){ df -k | grep root | awk '{printf ("%0.3f"), $4/$2 * 100}' } # Add in Temp as used before CPU_Temp() { /opt/vc/bin/vcgencmd measure_temp | cut -d"t" -f2 | cut -d"'" -f1 } echo "" >> "/home/astroberry/Astro/Z_PTS_Log.txt" echo "************************************************************************" >> "/home/astroberry/Astro/Z_PTS_Log.txt" echo "** New record of system info started at $(date '+%Y-%m-%d, %H:%M:%S') **" >> "/home/astroberry/Astro/Z_PTS_Log.txt" echo "** Total RAM available is $(RAM_Total_Num)B and total SWAP available is $(SWAP_Total_Num)B **" >> "/home/astroberry/Astro/Z_PTS_Log.txt" echo "** Total space on micro SD card (located @$(SD_Location)) is $(SD_Total_Num)GB **" >> "/home/astroberry/Astro/Z_PTS_Log.txt" echo "************************************************************************" >> "/home/astroberry/Astro/Z_PTS_Log.txt" for i in `seq 1 $SECONDS`; do echo $(date '+%Y-%m-%d %H:%M:%S') "T$(CPU_Temp)ºC. RAM parameters: Used:$(RAM_Used_Perc)%; Free:$(RAM_Free_Perc)%; Shared:$(RAM_Shared_Perc)%; Buffer:$(RAM_Buf_Perc)%; Cache:$(RAM_Cash_Perc)%; Available:$(RAM_Available_Perc)%. Swap used:$(SWAP_Used_Perc)%. µSD has $(SD_Available_Perc)% free space." >> "/home/astroberry/Astro/Z_PTS_Log.txt" sleep 1 if ((i == 1)); then # First iteration requires password. Simply print output to screen, with sudo so that password iteration timer is started. # PTS changed default timeout to 450min (7.5hrs) which should be long enough to complete most imaging. https://www.techrepublic.com/article/how-to-change-the-default-sudo-timeout/ echo "To avoid SUDO command for period of imaging, enter it below:" sudo echo "Ready to start KStars etc. Have fun!" elif free -w | awk '/Mem/ {exit !($4/$2 > 0.25)}'; then # FREE echo "Iteration $i; Free memory less than 10% threshold ($(RAM_Free_Perc)%). Cleaning memory in system." >> "/home/astroberry/Astro/Z_PTS_Log.txt" sudo sh -c 'echo 3 > /proc/sys/vm/drop_caches' elif free -w | awk '/Mem/ {exit !($7/$2 > 0.25)}'; then # CACHE echo "Iteration $i; Cache memory greater than 75% threshold ($(RAM_Cash_Perc)%). Cleaning memory in system." >> "/home/astroberry/Astro/Z_PTS_Log.txt" sudo sh -c 'echo 3 > /proc/sys/vm/drop_caches' elif free -w | awk '/Swap/ {exit !($3/$2 > 0.01)}'; then # SWAP echo "Iteration $i. Warning - SWAP memory used is non-zero: $(SWAP_Used_Perc)% used." >> "/home/astroberry/Astro/Z_PTS_Log.txt" echo "Iteration $i. Warning - SWAP memory used is non-zero: $(SWAP_Used_Perc)% used." fi done