Skip to content

Instantly share code, notes, and snippets.

@ading2210
Created January 21, 2025 03:23
Show Gist options
  • Save ading2210/882565526f7e1f2b9b14a022ac3741ac to your computer and use it in GitHub Desktop.
Save ading2210/882565526f7e1f2b9b14a022ac3741ac to your computer and use it in GitHub Desktop.
A bash script to limit ollama download speeds
#!/bin/bash
#see https://github.com/ollama/ollama/issues/2006 for why this is needed
set -e
run_nethogs() {
local pid="$1"
nethogs -t -d 0 -P "$pid" | grep --line-buffered "ollama"
}
find_ollama() {
ps -ef | grep "ollama serve" | grep -v "grep" | awk '{print $2}'
}
limit_bandwidth() {
local max_rate="$1"
local pid="$(find_ollama)"
if [ ! "$pid" ]; then
echo "error: ollama not running"
return 1
fi
run_nethogs "$pid" | while read line; do
local rate="$(echo "$line" | awk '{print $NF}')"
local rate_int="${rate%.*}"
printf "\r$rate_int KB/s "
if [ "$rate_int" -gt "$max_rate" ]; then
kill -STOP "$pid"
else
kill -CONT "$pid"
fi
done
}
if [ ! "$1" ]; then
echo "usage: ./ollama-limiter.sh [rate]"
exit 1
fi
limit_bandwidth "$1"
@oxbambooxo
Copy link

In mac os, i ran into this problem: "Error reading /proc, needed to get pid set"

@krnlskwd
Copy link

Works well. Thanks for sharing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment