Skip to content

Instantly share code, notes, and snippets.

@arturmartins
Last active June 28, 2025 12:04
Show Gist options
  • Save arturmartins/62653143b9a384e63e79bb81d8fe9386 to your computer and use it in GitHub Desktop.
Save arturmartins/62653143b9a384e63e79bb81d8fe9386 to your computer and use it in GitHub Desktop.
Shell script to update all opkg upgradable packages.

Upgrade all opkg packages for your OpenWRT Router

How to install:

Add the script to the /usr/bin folder

As root, run:

wget -O /usr/bin/upgrade-all https://gist.githubusercontent.com/arturmartins/62653143b9a384e63e79bb81d8fe9386/raw/upgrade-all.sh
chmod +x /usr/bin/upgrade-all

Run the script as root:

upgrade-all

Ouput:

[upgrade-all] Skipping update: Last update was less than 1 hour ago.
[upgrade-all] Checking for upgradable packages...
[upgrade-all] All packages are up to date.
#!/bin/sh
#
# Updates all upgradable opkg packages.
#
# Author: Artur Martins <[email protected]>
# Version: 1.1
# Date: 2025-Jun-28
#-----------------------------------------------------------------
# Changelog:
# 1.1 - Added verbosity
# 1.0 - First edition
##################################################################
log() {
echo "[$(basename "$0")] $1"
}
STAMP_FILE="/tmp/opkg_update_timestamp"
MAX_AGE=3600 # in seconds
should_update() {
if [ ! -f "$STAMP_FILE" ]; then
return 0 # No stamp file, update needed
fi
LAST_UPDATE=$(cat "$STAMP_FILE")
NOW=$(date +%s)
AGE=$((NOW - LAST_UPDATE))
[ "$AGE" -ge "$MAX_AGE" ]
}
if should_update; then
log "Updating package lists..."
opkg update
date +%s > "$STAMP_FILE"
else
log "Skipping update: Last update was less than 1 hour ago."
fi
log "Checking for upgradable packages..."
UPGRADABLE=$(opkg list-upgradable | cut -f 1 -d ' ')
if [ -n "$UPGRADABLE" ]; then
log "Packages to upgrade:"
echo "$UPGRADABLE"
log "Starting upgrade process..."
echo "$UPGRADABLE" | xargs opkg upgrade --verbosity=2
log "Upgrade complete."
else
log "All packages are up to date."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment