Skip to content

Instantly share code, notes, and snippets.

@Thell
Last active March 19, 2022 19:16
Show Gist options
  • Save Thell/7350ee9319a2967e9a0d to your computer and use it in GitHub Desktop.
Save Thell/7350ee9319a2967e9a0d to your computer and use it in GitHub Desktop.
Aria2 rpc based progress monitor for apt-fast in non-interactive shells.
#!/bin/sh
# Aria2 rpc based progress output for apt-fast
# trigger using --on-download-complete callback.
# Provide bytesNeeded in the parent environment...
# apturis=$(apt-get -qq --print-uris "$@")
# export DLLISTBYTES=$( echo "$apturis"| awk -F " " '{bytes+=$3} END{print bytes}' )
aria2_rpc () {
http --ignore-stdin localhost:6800/jsonrpc id=apt-fast method=aria2.$1
}
export $( aria2_rpc getGlobalStat | jq -r '.result|to_entries[]|join("=")' )
bytesNeeded=$DLLISTBYTES
bytesDownloaded=$(du -bcs $(dirname -- $3)/* | tail -1 | cut -f1)
numUris=$(( numActive + numStopped + numWaiting ))
timeElapsed=$(ps -p $(pgrep -f '^aria2c*' --oldest) -o etime= | tr -d " ")
if [ "$numStopped" = "$numUris" ]; then
# Shutdown aria2 when queue is exhausted.
printf \
"Pre-Fetched %s [%s] in %s (%s) from %s packages.$(tput el)\n" \
$( numfmt --to si --suffix="B" --round nearest $bytesDownloaded ) \
$( numfmt --to iec-i --suffix="B" --round nearest $bytesDownloaded ) \
$timeElapsed \
$( numfmt --to iec-i --suffix="B/s" $downloadSpeed ) \
$numUris
aria2_rpc shutdown > /dev/null
else
exec 1>&2
printf \
"\r%s %3s%% [ %s / %s @ %s ][ W: %d A: %d S: %d / %d (%s%%) ]$(tput el)" \
$timeElapsed \
$(( bytesDownloaded * 100 / bytesNeeded )) \
$( numfmt --to iec-i --suffix="B" --round nearest $bytesDownloaded ) \
$( numfmt --to iec-i --suffix="B" --round nearest $bytesNeeded ) \
$( numfmt --to iec-i --suffix="B/s" $downloadSpeed ) \
$numWaiting \
$numActive \
$numStopped \
$numUris \
$(( numStopped * 100 / numUris ))
fi
@Thell
Copy link
Author

Thell commented Oct 1, 2015

  • Get download path from completed file argument.
  • output intermediate progress info to stderror
  • give final output on stdout

@nippurl
Copy link

nippurl commented Jun 29, 2020

hi thell, how could apt-fast-progress be used?
An example??

@Thell
Copy link
Author

Thell commented Jun 29, 2020

See https://github.com/Thell/ddash along with some usage in the dockerfiles associated with https://github.com/Thell/dockerfiles/tree/master/scripts.

Good Luck! Have Fun!

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