Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save paulgibbs/aae75161f63eb61ddfdbf98cd1055bda to your computer and use it in GitHub Desktop.

Select an option

Save paulgibbs/aae75161f63eb61ddfdbf98cd1055bda to your computer and use it in GitHub Desktop.
remove stuck cron
#!/bin/bash
HOOK="edacm_licence_check"
BACKUP_DIR="./cron-backups"
mkdir -p "$BACKUP_DIR"
echo "==============================="
echo "WordPress Multisite Cron Cleaner"
echo "Hook: $HOOK"
echo "==============================="
SITES=$(wp site list --field=url --format=csv)
for URL in $SITES; do
echo ""
echo "--- Site: $URL ---"
BACKUP_FILE="$BACKUP_DIR/cron-backup-$(echo $URL | sed 's/[^a-zA-Z0-9]/_/g').json"
wp option get cron --format=json --url="$URL" > "$BACKUP_FILE" 2>/dev/null
echo "Backup saved to $BACKUP_FILE"
wp eval '
$hook = "'"$HOOK"'";
// Clear caches before reading
wp_cache_delete("cron", "options");
wp_cache_delete("alloptions", "options");
$crons = get_option("cron");
if (!is_array($crons)) { echo "No cron array found.\n"; return; }
$removed = 0;
foreach ($crons as $timestamp => $hooks) {
if (!is_array($hooks)) continue;
if (isset($hooks[$hook])) {
$removed += count($hooks[$hook]);
unset($crons[$timestamp][$hook]);
if (empty($crons[$timestamp])) {
unset($crons[$timestamp]);
}
}
}
if ($removed > 0) {
$result = update_option("cron", $crons);
wp_cache_delete("cron", "options");
wp_cache_delete("alloptions", "options");
echo $result ? "Removed {$removed} instance(s).\n" : "update_option returned false (value may be unchanged or cache stale).\n";
} else {
echo "Nothing to remove.\n";
}
' --url="$URL" --skip-plugins --skip-themes
done
echo ""
echo "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment