How to: run a speed test every hour and log it into a file on Linux

Automating speed test to diagnose any potential networking issues such as congestion or capacity has never been easier. Nowadays, there are many solutions available that can be utilized whether on your internal LAN or through the internet. The easiest way of gauging your internet connection speed would be from speedtest.net. Here’s how you can set it up over on a Linux machine.

Speedtest CLI

Fortunately, speedtest.net by Ookla has a Linux version available for the shell called Speedtest CLI. This makes it very convenient to run a speed test and manipulate the data in anyway you like. For this demonstration we are simply going to take the output of the speed test and direct it to a file for later viewing.

First, lets head on over to the https://www.speedtest.net/apps/cli and choose your preferred Linux distribution. You will then need to create a bash script and insert the following into the file based on your distribution:

Debian/Ubuntu

sudo apt-get install gnupg1 apt-transport-https dirmngr
export INSTALL_KEY=379CE192D401AB61
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys $INSTALL_KEY
echo "deb https://ookla.bintray.com/debian generic main" | sudo tee  /etc/apt/sources.list.d/speedtest.list
sudo apt-get update
apt-get install speedtest

Fedora/CentOS/Redhat

sudo yum install wget
wget https://bintray.com/ookla/rhel/rpm -O bintray-ookla-rhel.repo
sudo mv bintray-ookla-rhel.repo /etc/yum.repos.d/
sudo yum install speedtest

When you’re ready, simply make it an executable with:

chmod +x ./installer_speedtest.sh

Run the script to install Speedtest CLI.

How to use

The speedtest program is very easy to use. All you have to do is run the following to do a simple speed test:

speedtest

This will pick the closest speedtest.net server and measure your latency, download, and upload. If you would like to see a list of servers available nearby, you can do it like this:

speedtest -L

You will then see a list of servers and their respective IDs. This is important if you were to want to only use speedtest with a specific server with:

speedtest -s IDofserver

It is recommended to use a specific server when running your script as it will help you produce accurate results.

We are now going to create a script to run daily for our speedtest, this script will be placed in the /etc/cron.hourly directory:

nano /etc/cron.hourly/speedtest.sh

Now we will add the following to our bash script:

#!/bin/bash

/bin/echo "`date`:" >> /path/to/speedtestlog
/bin/speedtest -s ID >> /path/to/speedtestlog

Here we are using the full path to our programs. Do note that this may vary from Linux distribution and you will need to check with whereis speedtest && whereis echo to find the full path to the binary files. The first part will print our date and time to the speedtestlog file. Next, the speedtest will be ran and placed right below the date/time.

Make sure you chmod +x your file and then run it with cd /etc/cron.hourly && ./speedtest.sh.

In order to display your speedtestlog file, you can either:

cat /path/to/speedtestlog

Or

less /path/to/speedtestlog

This is by far the easiest way of gauging your connection speed through the internet.