Skip to content

Instantly share code, notes, and snippets.

@SamadiPour
Created August 17, 2024 19:52
Show Gist options
  • Save SamadiPour/a719ad5bd481aa9fb6d1bf75b4d33421 to your computer and use it in GitHub Desktop.
Save SamadiPour/a719ad5bd481aa9fb6d1bf75b4d33421 to your computer and use it in GitHub Desktop.
A bash script for retrying the failed tasks in Aria2
#!/bin/bash
ARIA_URL="http://127.0.0.1:6800/jsonrpc"
SECRET="secret"
# Step 1: Retrieve GIDs of failed downloads
response=$(curl -s -X POST $ARIA_URL \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 1,
"method": "aria2.tellStopped",
"params": ["token:'"$SECRET"'", 0, 1000]
}')
# Check if success
if [ $? -ne 0 ]; then
echo "Failed to retrieve GIDs of failed downloads"
exit 1
fi
# Loop through each GID and retry the download
lenght=$(echo "$response" | jq -r '.result | length')
for ((i = 0; i < $lenght; i++)); do
GID=$(echo "$response" | jq -r '.result['"$i"'].gid')
URL=$(echo "$response" | jq -r '.result['"$i"'].files[0].uris[0].uri')
STATUS=$(echo "$response" | jq -r '.result['"$i"'].status')
if [ "$STATUS" != "error" ]; then
echo "Skipping: $GID $URL $STATUS"
continue
fi
echo "Retrying: $GID $URL $STATUS"
task_status=$(curl -s -X POST $ARIA_URL \
-H 'Content-Type: application/json' \
-d '{
"jsonrpc": "2.0",
"id": 2,
"method": "system.multicall",
"params": [[{"methodName":"aria2.tellStatus","params":["token:'"$SECRET"'","'"$GID"'"]},{"methodName":"aria2.getOption","params":["token:'"$SECRET"'","'"$GID"'"]}]]
}')
params=$(echo $task_status | jq -c '.result[1][0]')
# Remove the failed download result
curl -s -X POST $ARIA_URL \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 2,
"method": "aria2.removeDownloadResult",
"params": ["token:'"$SECRET"'", "'"$GID"'"]
}'
# Re-add the download
curl -s -X POST $ARIA_URL \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 3,
"method": "aria2.addUri",
"params": ["token:'"$SECRET"'",["'"$URL"'"],'"$params"']
}'
done
@SamadiPour
Copy link
Author

Cronjob for starting at 2:02 and ending at 7:58

02 2 * * * curl http://127.0.0.1:9090/jsonrpc -H "Content-Type: application/json" -H "Accept: application/json" --data '{"jsonrpc": "2.0","id":1, "method": "aria2.unpauseAll", "params":["token:secret"]}'

58 7 * * * curl http://127.0.0.1:9090/jsonrpc -H "Content-Type: application/json" -H "Accept: application/json" --data '{"jsonrpc": "2.0","id":1, "method": "aria2.pauseAll", "params":["token:secret"]}'

Cronjob for retrying failed ones every 10 minutes from 2:00 to 8:00

0,10,20,30,40,50 2-7 * * * bash retry.sh

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