Created
January 21, 2025 03:23
-
-
Save ading2210/882565526f7e1f2b9b14a022ac3741ac to your computer and use it in GitHub Desktop.
A bash script to limit ollama download speeds
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In mac os, i ran into this problem: "Error reading /proc, needed to get pid set"