#!/usr/bin/env bash set -eu -o pipefail SECONDS=${1:-1000000} # Report system stats to text file each second for reference. # Ideally want to start the script via terminal so that closing the terminal will stop writing the file! # Otherwise it'll just keep writing with no limits until RPi is restarted! # Example of what gets printed out: # 2022-03-26 07:18:44; Temp=39.9ºC. RAM 'Used': 399MB (10.5%); RAM 'Free': 2.4GB (64.7%); RAM 'Available': 3.1GB (84.6%); RAM used in Shared & Buffer: 26.0%. Swap used: 0.0%. µSD @/dev/root uses 20,178,276kB. # HDPARM added to RPi to check out the speeds of writing. # https://www.youtube.com/watch?v=Tgrka088ZFk # Shown to run at 40MB/s with RPi and MMC (SSD is upwards of 240MB/s if used... # sudo hdparm -t --direct /dev/mmcblk0 ## OTHER potential interesting bits: #actual_cpu_clock_speed() { # /opt/vc/bin/vcgencmd measure_clock arm | cut -d"=" -f2 | cut -d"'" -f1 #} #desired_cpu_clock_speed() { # cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq #} # All memory (note the 'Gi' may need further cropping. # free -h | cut -b 1-6,15-19,27-31,39-43,50-55,58-67,71-79 # free -h | head -n 2 | cut -b 1-6,15-20,27-33,71-79 # Alternative - Total,Used,Available... #** Below cut each of these and assign to the titles given by "free -h" **# CPU_Temp() { /opt/vc/bin/vcgencmd measure_temp | cut -d"t" -f2 | cut -d"'" -f1 } RAM_Total_Num() { free -h | tail -n 2 | head -n 1 | cut -b 15-18 } RAM_Used_Num() { free -h | tail -n 2 | head -n 1 | cut -b 27-30 } RAM_Free_Num() { free -h | tail -n 2 | head -n 1 | cut -b 39-42 } RAM_Shared_Num() { free -h | tail -n 2 | head -n 1 | cut -b 50-55 } RAM_BufCash_Num() { free -h | tail -n 2 | head -n 1 | cut -b 58-67 } RAM_Available_Num() { free -h | tail -n 2 | head -n 1 | cut -b 75-78 } SWAP_Total_Num() { # Swap is HDD memory used as RAM. Used SWAP should be as low as possible. free -h | tail -n 1 | cut -b 15-18 } SWAP_Used_Num() { free -h | tail -n 1 | cut -b 27-30 } SD_Location() { df -k | head -n 2 | tail -n 1 | cut -b 1-9 } SD_Used_NumA() { # Given in kB, so split so easy to add commas and read in output df -k | head -n 2 | tail -n 1 | cut -b 26-27 } SD_Used_NumB() { df -k | head -n 2 | tail -n 1 | cut -b 28-30 } SD_Used_NumC() { df -k | head -n 2 | tail -n 1 | cut -b 31-33 } # Calculate out percentages here RAM_Used_Perc() { free | grep Mem | awk '{printf ("%4.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 | grep Mem | awk '{printf ("%4.1f"), $4/$2 * 100.0}' } RAM_Available_Perc() { free | grep Mem | awk '{printf ("%4.1f"), $7/$2 * 100.0}' } RAM_Others_Perc() { free | grep Mem | awk '{printf ("%4.1f"), ($5 + $6)/$2 * 100.0}' } SWAP_Used_Perc() { free | grep Swap | awk '{printf ("%4.1f"), $3/$2 * 100.0}' } # Following variables available from the above coding: # CPU_Temp, RAM_Total_Num, RAM_Used_Num, RAM_Free_Num, RAM_Shared_Num, RAM_BufCash_Num, RAM_Available_Num, SD_Location, SD_Used_Num(A-C) # SWAP_Total_Num, SWAP_Used_Num, RAM_Used_Perc, RAM_Free_Perc, RAM_Available_Perc, RAM_Others_Perc, SWAP_Used_Perc echo "" >> "/home/astroberry/Astro/SystemLog.txt" echo "*************************************************************************" >> "/home/astroberry/Astro/SystemLog.txt" echo "** New record of system info started at $(date '+%Y-%m-%d, %H:%M:%S') **" >> "/home/astroberry/Astro/SystemLog.txt" echo "** Total RAM available is $(RAM_Total_Num)B and total SWAP available is $(SWAP_Total_Num)B. **" >> "/home/astroberry/Astro/SystemLog.txt" echo "*************************************************************************" >> "/home/astroberry/Astro/SystemLog.txt" for i in `seq 1 $SECONDS`; do echo $(date '+%Y-%m-%d %H:%M:%S') "T$(CPU_Temp)ºC. RAM 'Used': $(RAM_Used_Num)B ($(RAM_Used_Perc)%); RAM 'Free': $(RAM_Free_Num)B ($(RAM_Free_Perc)%); RAM 'Available': $(RAM_Available_Num)B ($(RAM_Available_Perc)%); RAM used in Shared & Buffer: $(RAM_Others_Perc)%. Swap used:$(SWAP_Used_Perc)%. µSD @$(SD_Location) uses $(SD_Used_NumA),$(SD_Used_NumB),$(SD_Used_NumC)kB." >> "/home/astroberry/Astro/SystemLog.txt" sleep 1 done