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.
 

Notes

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.
 

Installation

  1. Switch to the root user:

    sudo su
  2. Install speedtest-cli, run each line separately:

    sudo apt-get install python-pip
    sudo pip install speedtest-cli
  3. Download the speedtest-csv script. 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
  4. Now give the file execute permissions:

    sudo chmod +x speedtest-csv
  5. 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:

    cmd="/usr/local/bin/speedtest-cli $opts"

    save the file and exit.

  6. Next move speedtest-csv to /usr/bin/:

    sudo mv speedtest-csv /usr/bin/
  7. 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
  8. Generate the CSV header:

    sudo speedtest-csv --header >> /cron/speedtest/speedtest.csv
  9. Add to the CRON:

    sudo crontab -e
  10. 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.

Comments

alex
  • March 16th, 2019

In step 7.Generate the CSV header:
I get “/cron/speedtest/speedtest.csv: file or foilder does not exist”

Ryan
  • March 16th, 2019

Hello,

You will have to ensure the /cron/speedtest/ folders already exist. I have amended the instructions now.

Try this:

sudo mkdir -p /cron/speedtest

KNB
  • May 20th, 2020

Super , really worked well for me. Thank you !!

Ryan
  • May 20th, 2020

Thanks, I’m glad to hear 🙂

Maja Minnaert
  • November 6th, 2020

Hi!

When attempting step 8 of this process,
“sudo speedtest-csv –header >> /cron/speedtest/speedtest.csv”

I keep running into the error:
“bash: /cron/speedtest/speedtest.csv: Permission denied”

I have tried:
– putting the path in my home folder ( ~/cron/speedtest )
– first creating the file with touch ( cd /cron/speedtest; touch speedtest.csv )
– finding out how the permissions work and adapting those settings ( i found a command line going “chmod u+x /cron/speedtest/speedtest.csv”. This gives a “Permission denied” error too.

I have also:
– googled, looked on stackoverflow, …

I seem to be missing something, be it complex or simple. Any way you could help?

Kind regards
Maja Minnaert

Ryan
  • November 8th, 2020

Hi Maja,

I had the same issue a few weeks ago with this, which had been installed been working fine since 2018.

It turned out, updating Python solved the issue. Please can you try running: “sudo apt-get –only-upgrade install python-pip && sudo apt-get install python-pip” and restart your computer.

I have tried this tutorial to be sure everything is still correct. Run on a clean; up-to-date install of Ubuntu 18.04 and it still works okay.

Thanks

Mr Ian Bunyard
  • January 19th, 2021

Hi Ryan, I am very new to using the Raspberry Pi and to the use of Linux in particular. I am interested in this application and I have followed the instructions in the Repo but have run into a problem of permission denied when executing the command “sudo speedtest-csv –header >> /cron/speedtest/speedtest.csv”

I have seen you earlier recommendation and I do have the latest version of python-pip installed as confirmed when I ran the instruction “sudo apt-get –only-upgrade install python-pip && sudo apt-get install python-pip. Do you have any suggestions on how permission can be obtained. Thanks Ian Bunyard.

Ryan
  • January 19th, 2021

Hi Ian,

I have seen a few other people say they had a permission denied error. However I tried to replicate this with a fresh install of Ubuntu and did not experience the same issue.

Could you check if your user account has sudo privileges please. Run this command in the terminal, and see if ‘sudo’ is returned:

groups

If not, you will have to give your user sudo privileges. See this post for more info: https://chewett.co.uk/blog/673/grant-sudo-access-user-raspberry-pi-raspbian/

Post a Comment