How to Gracefully Stop aria2 and Save Session State?

This article provides a quick overview and practical guide on how to safely terminate an active aria2c download process without losing your progress. When managing large downloads or multiple files via the command line, abruptly killing the process can corrupt your session. By using specific termination signals or configuring automated session saving, you can ensure that aria2 saves its current state to a file, allowing you to resume your downloads seamlessly the next time you start the application.

Understanding the Graceful Shutdown

To prevent data loss, aria2 needs to receive a specific signal that tells it to pause active downloads, flush its cache to the disk, and write the current progress to your specified session file.

If you force-close the terminal window or use a harsh termination command, aria2 will exit immediately, and any progress made since the last automated save point will be lost.

Using the Correct Command Line Signals

The most straightforward way to gracefully stop aria2 depends on how you are currently running it:

To safely kill a background process, first find the Process ID (PID) and then send the signal:

kill -15 <PID>

(Note: -15 corresponds to SIGTERM, which allows the program to perform its cleanup routines).

Warning: Avoid using kill -9 <PID> (SIGKILL). This forcefully kills the process instantly and guarantees that your session state will not be saved.

Configuring Automated Session Saving

For a graceful shutdown to work effectively, aria2 must be told where to save its state. You can configure this by using two essential command-line flags when you start your download:

You can combine these flags in your startup command like this:

aria2c --input-file=/path/to/session.txt --save-session=/path/to/session.txt https://example.com/largefile.zip

Saving Progress Periodically

As an extra layer of security against unexpected system crashes or power outages, you can instruct aria2 to save its session state automatically at regular intervals.

Use the --save-session-interval=<SECONDS> flag to define how often the state should be written to the disk. For example, to save your progress every 60 seconds, use the following command:

aria2c --input-file=/path/to/session.txt --save-session=/path/to/session.txt --save-session-interval=60 https://example.com/largefile.zip

By combining regular interval saving with the proper Ctrl + C or SIGTERM shutdown methods, you ensure your download progress remains intact under almost any circumstances.