Skip to content

Instantly share code, notes, and snippets.

@alainwolf
Created April 1, 2025 16:01
Show Gist options
  • Save alainwolf/7d051fe77d823d048a7defae11f2c0a1 to your computer and use it in GitHub Desktop.
Save alainwolf/7d051fe77d823d048a7defae11f2c0a1 to your computer and use it in GitHub Desktop.
Override of the default `librespeed` cron job command as provided by Turris Omnia
#!/usr/bin/env bash
# *****************************************************************************
# Override of the default `librespeed` cron job command as provided by
# Turris Omnia
# See https://forum.turris.cz/t/turris-os-7-1-4-is-out/21639/12?u=milkandhoney
#
# Required packages: coreutils-date, jq
# opkg update && opkg install coreutils-date jq
#
# *****************************************************************************
# Settings
cmd="/usr/bin/librespeed-cli"
data_dir="/srv/librespeed/data"
local_json="/srv/librespeed/servers.json"
# server_id=10 # Zurich, Switzerland, Michael Stapelberg
# Generate dynamic directory and filename based on the current date and time
current_month=$(date +"%Y-%m")
current_time=$(date +"%FT%T.%N%:z")
result_dir="$data_dir/$current_month"
result_file="$result_dir/$current_time.json"
# Ensure the directory exists
mkdir -p "$result_dir"
# Check if a local JSON server list file is provided
if [ -z "$local_json" ]; then
# echo "No local server list file provided. Using default built-in server list."
true
else
# Check if the local JSON file exists and is readable
if [ ! -r "$local_json" ]; then
# echo "Warning: Local JSON file $local_json not found or not readable. Using default built-in server list."
local_json=""
else
# Check if the local JSON file is a valid JSON file
if ! jq empty "$local_json" >/dev/null 2>&1; then
# echo "Warning: Local JSON file $local_json is not a valid JSON file. Using default built-in server list."
local_json=""
else
# echo "Using local JSON file $local_json."
cmd+=("--local-json" "$local_json")
fi
fi
fi
# Check if the server ID var is set
if [ -z "$server_id" ]; then
# echo "No server ID provided. Selecting the fastest server based on ping."
true
else
# Check if the server ID is a valid number
if ! [[ "$server_id" =~ ^[0-9]+$ ]]; then
# echo "Warning: Server ID $server_id is not a valid number. Selecting the fastest server based on ping."
server_id=""
else
# Check if the server ID is found in the JSON file
if jq -e ".[] | select(.id == $server_id)" "$local_json" >/dev/null; then
# If the server ID is found, use it
# echo "Using server ID $server_id from $local_json."
cmd+=("--server" "$server_id")
else
# If the server ID is not found
# echo "Warning: Server ID $server_id not found in $local_json. Selecting the fastest server based on ping."
server_id=""
fi
fi
fi
# Set output format to JSON
cmd+=("--json")
# Run the command and redirect output
"${cmd[@]}" >"$result_file"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment