This is a quick little guide to setup a periodic job on MAC for measuring internet speed (Download & Upload) and capture the output as a csv file. The job will execute whenever your machine is on and keep collecting internet speed statistics in a csv file. Please follow below steps for setting this up.
[Note: Tested and working fine on MacBook Pro with macOS Catalina 10.15.7]
Install Brew
Brew is a utility for MAC that allows installing packages. We need this to install the speedtest utility package. To setup brew, fire up a terminal and copy-paste below single line command -
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
It will ask for your permission and password a few times.
Install Speedtest Command Line Utility
Execute below three commands in that order to install speedtest utility
brew tap teamookla/speedtest
brew update
brew install speedtest --force
Create Shell Script 'speedtest.sh'
#!/bin/bash/usr/local/bin/speedtest -p no | grep -i 'Download\|Upload' > .tmp-bandwidth.txtif [ $? -ne 0 ]; thenecho `date`,'0','0'elsedownload_speed=`cat .tmp-bandwidth.txt | grep -i 'download' | awk -F" " '{ print $3}'`upload_speed=`cat .tmp-bandwidth.txt | grep -i 'upload' | awk -F" " '{ print $3}'`echo `date`,$download_speed,$upload_speedfi
Schedule the Script
Set the cron schedule
To schedule script to run every 10 minutes, copy-paste below single-line command. (Entering this command may trigger a pop-up asking permissions, say 'OK' in that case.)
echo "*/10 * * * * cd ~/Desktop && bash speedtest.sh >> speedtest-results.csv" | crontab -
You can change the job execution interval minutes to a desired frequency by replacing '10' in above command.
Allow cron access disk
To be able to access the shell script and write to the CSV file, cron needs to be given permissions. Simply follow below steps -
- Open 'System Preferences' and then Click "Security & Privacy"
- Select 'Full Disk Access' from the list and at the bottom, click on 'Click the lock to make changes'. Enter your mac account password when prompted.
- Now you need to add 'cron' in the right side list. Click the '+' icon.
- This will open up a file browser. Now press "Command + Shift + G" and enter '/usr/sbin/cron' in the textbox and click 'Go', and you are done!
The Output
Timetamp, Download speed in Mpbs, Upload speed in Mpbs.
Sat Sep 25 19:57:45 IST 2021,18.79,7.26Sat Sep 25 20:07:43 IST 2021,8.00,6.19Sat Sep 25 20:17:43 IST 2021,11.13,7.01
If internet is down or any error occurs, the bandwidth will be shown as 0,0 for that particular time
You can also open this file in Microsoft Excel and generate a nice graph out of it!
No comments:
Post a Comment