Sunday, 3 May 2020

Bash scrip to check internet speed frequently in Linux

Check internet speed frequently

$ crontab -l
   # set frequency
   20,50 * * * * sh /usr/local/src/internet_speed_test.sh

$ vim internet_speed.sh
   #!/bin/bash
   # run python to get speed
   python speedtest.py > /var/log/speed_test.log

   # process speed_test.log file
   date=$(date "+%d %B %Y %H:%M")
   echo -n "$date" >> /var/log/speed.log
 
   while IFS= read -r line
   do
     if [[ "$line" == *"Download"* ]] || [[ \n\t"$line" == *"Upload"* ]] ; then
           echo -n  '   ' "$line" >> /var/log/speed.log
     fi
   done < /var/log/speed_test.log
 
   echo -e "\n" >> /var/log/speed.log'

$ tail -f /var/log/speed.log
   03 May 2020 18:05    Download: 9.40 Mbit/s    Upload: 6.34 Mbit/s
   03 May 2020 18:22    Download: 11.34 Mbit/s    Upload: 6.12 Mbit/s

No comments:

Post a comment