aria2 vs. curl and wget: Which Downloader Is Right for You?

Automate Batch Downloads with aria2: Practical Examples and Scripts

aria2 is a lightweight, multi-protocol command-line downloader that supports HTTP(S), FTP, BitTorrent and Metalink. It’s ideal for automating batch downloads because it’s scriptable, supports segmented downloading, and offers a JSON-RPC interface for remote control. This article gives actionable examples and ready-to-run scripts to automate downloads with aria2 on Linux, macOS, or Windows (WSL/PowerShell).

1. Install aria2

  • Debian/Ubuntu:

    Code

    sudo apt update && sudo apt install aria2 -y
  • macOS (Homebrew):

    Code

    brew install aria2
  • Windows (chocolatey):

    Code

    choco install aria2

2. Basic batch download using a URI list

Create a text file urls.txt with one URL per line. Then run:

Code

aria2c -i urls.txt -j 5 -x 4 -s 4
  • -i: input file
  • -j 5: run up to 5 files in parallel
  • -x 4: max connections per server
  • -s 4: split a file into 4 segments

Save partials/resume: aria2 resumes automatically if files and .aria2 control files exist.

3. Download with output filenames and directory structure

Create urls.txt with lines in the format:

Code

Then:

Code

aria2c -i urls.txt –dir=/path/to/downloads –auto-file-renaming=false

This preserves specified filenames and folders.

4. Using Metalink or Torrent batches

Use –seed-time=0 to avoid long seeding if not desired:

Code

aria2c –seed-time=0 –dir=/downloads *.torrent

5. Scheduled/cron batch downloads

Example: daily cron job to download a list at 02:00 and log output. Create script /usr/local/bin/batch-download.sh:

Code

#!/bin/bash LOG=/var/log/aria2-batch-\((date +%F).log aria2c -i /home/user/urls.txt -j 5 -x 4 -s 4 --dir=/home/user/Downloads >> "\)LOG” 2>&1

Make executable:

Code

chmod +x /usr/local/bin/batch-download.sh

Cron entry (edit with crontab -e):

Code

0 2 * * * /usr/local/bin/batch-download.sh

6. Automate with a simple Bash wrapper (retry, logging, concurrency)

Script: batchretry.sh

Code

#!/bin/bash URLS=/home/user/urls.txt OUT=/home/user/Downloads LOGDIR=/var/log/aria2 mkdir -p “\(OUT" "\)LOGDIR” LOG=”\(LOGDIR/batch-\)(date +%F_%H%M%S).log”

MAXTRIES=3 for url in \((cat "\)URLS”); do attempt=1 while [ \(attempt -le \)MAXTRIES ]; do

aria2c -x 4 -s 4 -d "$OUT" "$url" >> "$LOG" 2>&1 && break attempt=$((attempt+1)) sleep $((attempt * 5)) 

done done

This downloads URLs one-by-one with retries; adapt to parallelism with GNU parallel if needed.

7. Use JSON-RPC for dynamic automation and remote control

Start aria2 daemon with RPC enabled:

Code

aria2c –enable-rpc –rpc-listen-all=false –rpc-allow-origin-all –rpc-listen-port=6800 –dir=/home/user/Downloads -D

Example Python script to add downloads via RPC (requires requests):

Code

import requests, json ARIA2_URL = ‘http://localhost:6800/jsonrpc’ def add_uri(uri, out=None):

params = [[uri], {"out": out}] if out else [[uri]] payload = {"jsonrpc":"2.0","method":"aria2.addUri","id":"qwer","params":params} r = requests.post(ARIA2_URL, json=payload) return r.json() 

print(adduri(”http://example.com/file.zip”,“file.zip”))

Use aria2’s methods: aria2.addUri, aria2.tellStatus, aria2.removeDownloadResult, aria2.pause, aria2.forcePause.

8. Windows PowerShell script (parallel downloads)

PowerShell example using Start-Process:

Code

\(urls = Get-Content .\urls.txt </span>\)aria2 = “C:\Program Files (x86)\aria2\aria2c.exe” \(downloadDir = "C:\Downloads" foreach (\)u in \(urls) { Start-Process -FilePath \)aria2 -ArgumentList “-d "$downloadDir"$u”” -NoNewWindow }

For better control, use the RPC approach from PowerShell using Invoke-RestMethod to call aria2 JSON-RPC.

9. Post-download hooks and notifications

Use –on-download-complete to run a script:

Code

aria2c -i urls.txt –on-download-complete=/usr/local/bin/after_download.sh

after_download.sh could move files, extract archives, or send a desktop notification / webhook.

Example afterdownload.sh:

Code

#!/bin/bash FILE=”\(3" # aria2 passes GID, FILE, ... mv "\)FILE” /mnt/storage/finished/ curl -X POST -H “Content-Type: application/json” -d “{\“file\”:\“$FILE\”}” https://example.com/webhook

10. Tips for reliable automation

  • Use absolute paths in scripts and cron.
  • Keep aria2 session file (–save-session) to resume across runs:

    Code

    aria2c –save-session=/home/user/aria2.session –input-file=/home/user/aria2.session
  • Limit disk and network I/O with –max-overall-download-limit and –max-overall-upload-limit.
  • Rotate logs and clean old .aria2 control files periodically.
  • Test scripts manually before scheduling.

Quick reference: common useful flags

  • –dir=PATH (download directory)
  • -i FILE (input file of URIs)
  • -j N (parallel downloads)
  • -x N (connections per server)
  • -s N (segments per file)
  • –enable-rpc (start RPC server)
  • –save-session=FILE and –input-file=FILE (session resume)
  • –on-download-complete=CMD (post-download hook)

Use these examples and scripts as templates — adapt paths, concurrency, and hooks to your environment for robust automated batch downloading with aria2.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *