Using Speedtest-CLI to record speed tests to CSV via CRON
One of the most common speed testing services for Internet connections is Ookla’s Speedtest, and more often than not your ISP will either throttle your connection, or their network will slow down at peak times.
Because of this, I wanted to make sure I am receiving the full connection speed which I am paying for.
I went looking for a solution and immediatly came across Speedtest-CLI by Matt Martz. This repo allows you to perform Speedtests through a command line interface.
But I was still looking for some sort of tool which could convert these results to a CSV file, so a new row can be appended to the file each time a test was run. Again, I went searching and came accross the fantastic Speedtest-CLI-Extras project by Henrik Bengtsson. This repo allows you to run the Speedtest CLI command but export the results to a CSV file. This will become useful to build graphs and check results over time.
So let’s start. The installation process is painless, you will require Python install (python-pip). These installation notes were tested on Ubuntu 18.04.
These instructions can be found in my Github repo here.
Switch to the
speedtest-cli, run each line separately:
sudo apt-get install python-pip sudo pip install speedtest-cli
speedtest-csvscript. You will also find a copy of this script within the repo which you can, or re-download from the original author and make the changes to the file mentioned in step 5.
sudo wget -O speedtest-csv https://raw.githubusercontent.com/HenrikBengtsson/speedtest-cli-extras/master/bin/speedtest-csv
Now give the file execute permissions:
sudo chmod +x speedtest-csv
You will need to edit a line in the file to so that it can be run from CRON:
sudo nano speedtest-csv
Find the line
cmd="speedtest-cli $opts"and add
/usr/local/bin/before it so it reads:
save the file and exit.
sudo mv speedtest-csv /usr/bin/
Create a folder for where your .csv file will be saved, I am saving mine within
/cron/speedtest/, but change this for whatever you’d like:
sudo mkdir -p /cron/speedtest
Generate the CSV header:
sudo speedtest-csv --header >> /cron/speedtest/speedtest.csv
Add to the CRON:
sudo crontab -e
Add the following line to the end of the file, this example is daily, ever four hours:
0 */4 * * * speedtest-csv >> /cron/speedtest/speedtest.csv
save the file and exit.