Skip to content

Instantly share code, notes, and snippets.

@briandfoy
Created January 20, 2020 00:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save briandfoy/ba285633dc01ea1b2cfc63adea5032b1 to your computer and use it in GitHub Desktop.
Save briandfoy/ba285633dc01ea1b2cfc63adea5032b1 to your computer and use it in GitHub Desktop.
#!/bin/sh
<< 'DOCS'
This is a small script I use to run speedtest from cron so I
can monitor some network performance. The speedtest command
comes from the speedtest-cli Python project:
https://github.com/sivel/speedtest-cli
This writes to the log file "speedtest.csv" or the file you specify in SPEEDTEST_LOG:
SPEEDTEST_LOG=/var/log/speedtest.csv ./speedtest-cron.sh
Each run can check several times, which might hit different
servers but also attempts to notice outliers (default is 3):
SPEEDTEST_REPEAT=5 ./speedtest-cron.sh
If the log file isn't there, the first run outputs the CSV
headers.
After a run, the script sleeps for a bit then runs another
test (default is 2m):
SPEEDTEST_SLEEP=300 ./speedtest-cron.sh
DOCS
REPEAT_TIMES=${SPEEDTEST_REPEAT:-3}
SLEEP_TIME=${SPEEDTEST_SLEEP:-120}
# https://github.com/sivel/speedtest-cli
# pip install speedtest-cli
COMMAND_NAME=speedtest
# https://wiki.bash-hackers.org/syntax/pe
OUTPUT_FILE=${SPEEDTEST_LOG:-speedtest.csv}
# https://stackoverflow.com/q/592620/2766176
COMMAND_PATH=$(command -v $COMMAND_NAME)
# https://stackoverflow.com/q/2990414/2766176
# >&2 echo "PATH is $COMMAND_PATH";
if [ ! -x "$COMMAND_PATH" ]; then
>&2 echo "Could not find the speedtest command: $COMMAND_PATH";
# http://tldp.org/LDP/abs/html/exitcodes.html
exit 127; # 127 = command not found
fi
# https://stackoverflow.com/q/169511/2766176
for i in $(seq 1 $REPEAT_TIMES); do
if [ -f "$OUTPUT_FILE" ]; then
$COMMAND_PATH --csv >> $OUTPUT_FILE
else
$COMMAND_PATH --csv-header >> $OUTPUT_FILE
$COMMAND_PATH --csv >> $OUTPUT_FILE
fi;
sleep $SLEEP_TIME
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment