Unrar Password Protected Archive in a Cron Job
Automating the extraction of password-protected RAR archives requires
combining the unrar utility with the Linux
cron daemon. This guide covers how to formulate the
non-interactive unrar command using the password flag,
configure the execution environment, and set up a scheduled cron job to
run the extraction automatically without user intervention.
1. The Non-Interactive
unrar Command
Cron jobs execute without an interactive terminal (TTY), meaning any command that prompts for input will hang indefinitely. To extract a password-protected archive automatically, you must pass the password directly and suppress interactive prompts.
The basic command syntax is:
/usr/bin/unrar x -p"YourPassword" -y -o+ /path/to/archive.rar /path/to/destination/Key flags used:
x: Extracts files with their full directory path structure.-p"YourPassword": Provides the archive password. Note that there is no space between-pand the password string.-y: Automatically answers "yes" to all interactive queries.-o+: Overwrites existing files automatically to prevent prompt freezes.
Note: Ensure you have the non-free version of unrar
installed (sudo apt install unrar on Debian/Ubuntu), as the
open-source unrar-free tool does not support all modern RAR
compression algorithms or password formats.
2. Creating a Shell Script
While you can put the command directly into your crontab, wrapping it
inside a shell script is safer and easier to maintain. Cron runs with a
restricted environment and limited PATH variables.
Create a script, for example
/home/user/scripts/extract_archive.sh:
#!/bin/bash
# Define absolute paths
UNRAR_BIN="/usr/bin/unrar"
ARCHIVE="/home/user/backups/data.rar"
DEST_DIR="/home/user/extracted/"
PASSWORD="YourArchivePassword"
LOG_FILE="/home/user/logs/unrar.log"
# Create destination and log directories if they don't exist
mkdir -p "$DEST_DIR"
mkdir -p "$(dirname "$LOG_FILE")"
# Execute the extraction and append output to log
if [ -f "$ARCHIVE" ]; then
echo "[$(date)] Starting extraction of $ARCHIVE" >> "$LOG_FILE"
$UNRAR_BIN x -p"$PASSWORD" -y -o+ "$ARCHIVE" "$DEST_DIR" >> "$LOG_FILE" 2>&1
echo "[$(date)] Extraction completed." >> "$LOG_FILE"
else
echo "[$(date)] Error: File $ARCHIVE does not exist." >> "$LOG_FILE"
fiMake the script executable:
chmod +x /home/user/scripts/extract_archive.sh3. Scheduling the Cron Job
Open your user crontab editor:
crontab -eAdd a scheduled entry pointing to your script using an absolute path. For example, to run the extraction every night at 2:00 AM:
0 2 * * * /home/user/scripts/extract_archive.sh
If you prefer to run the command directly inside the crontab without a wrapper script:
0 2 * * * /usr/bin/unrar x -p"YourPassword" -y -o+ /home/user/archive.rar /home/user/destination/ >> /home/user/unrar.log 2>&1
4. Security Considerations
Hardcoding passwords in crontab or unencrypted scripts exposes credentials to anyone with access to the system process list or file read permissions. To minimize risk, restrict file permissions on your script:
chmod 700 /home/user/scripts/extract_archive.shThis ensures only the file owner can read or execute the script containing the password.