From ca1e0b1be03fee6a90eb4e7f104352c163f6eaa5 Mon Sep 17 00:00:00 2001 From: Vitaly Turovsky Date: Thu, 3 Jul 2025 17:25:18 +0300 Subject: [PATCH] should fix cron --- README.md | 2 ++ deploy.sh | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 80 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ffc3446..e4b002c 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,8 @@ chmod +x deploy.sh ./deploy.sh ``` +If you enabled automatic updates, ensure cron is running: `crontab -l`. If not, run `./deploy.sh --update-cron` to setup it again. + ## ✨ Features ### 🎯 Available Now diff --git a/deploy.sh b/deploy.sh index 861e0d7..41ab88c 100644 --- a/deploy.sh +++ b/deploy.sh @@ -366,7 +366,16 @@ EOF # Setup cron job CRON_JOB="0 2 * * * $UPDATE_SCRIPT >> /var/log/mwc-update.log 2>&1" - (crontab -l 2>/dev/null | grep -v mwc-update; echo "$CRON_JOB") | crontab - + # Create a temporary file for the new crontab + TEMP_CRONTAB=$(mktemp) + # Get existing crontab if any, excluding old update script entries + (crontab -l 2>/dev/null || echo "") | grep -v mwc-update > "$TEMP_CRONTAB" + # Add new update script entry + echo "$CRON_JOB" >> "$TEMP_CRONTAB" + # Install new crontab + crontab "$TEMP_CRONTAB" + # Clean up + rm -f "$TEMP_CRONTAB" print_success "Automatic updates configured." print_info "Updates will check daily at 2:00 AM" @@ -488,10 +497,78 @@ cleanup() { fi } +# Setup cron job +setup_cron() { + set -x # Enable debug output + print_step "Setting up cron job..." + + CRON_JOB="0 2 * * * /usr/local/bin/mwc-update.sh >> /var/log/mwc-update.log 2>&1" + # Create a temporary file for the new crontab + TEMP_CRONTAB=$(mktemp) + if [ $? -ne 0 ]; then + print_error "Failed to create temporary file" + return 1 + fi + print_info "Created temp file: $TEMP_CRONTAB" + + # Get existing crontab if any, excluding old update script entries + # First save current crontab or empty string if none exists + (crontab -l 2>/dev/null || echo "") > "$TEMP_CRONTAB" + if [ $? -ne 0 ]; then + print_error "Failed to get current crontab" + rm -f "$TEMP_CRONTAB" + return 1 + fi + + # Remove any existing update entries + if [ -s "$TEMP_CRONTAB" ]; then + grep -v "mwc-update" "$TEMP_CRONTAB" > "$TEMP_CRONTAB.tmp" && mv "$TEMP_CRONTAB.tmp" "$TEMP_CRONTAB" || true + fi + print_info "Current crontab saved (excluding old update entries)" + + # Add new update script entry + echo "$CRON_JOB" >> "$TEMP_CRONTAB" + if [ $? -ne 0 ]; then + print_error "Failed to add new cron job" + rm -f "$TEMP_CRONTAB" + return 1 + fi + print_info "Added new cron job entry" + + # Show what we're about to install + print_info "New crontab contents:" + cat "$TEMP_CRONTAB" + + # Install new crontab + crontab "$TEMP_CRONTAB" + if [ $? -ne 0 ]; then + print_error "Failed to install new crontab" + rm -f "$TEMP_CRONTAB" + return 1 + fi + print_info "Installed new crontab" + + # Clean up + rm -f "$TEMP_CRONTAB" + set +x # Disable debug output + + print_success "Cron job setup completed." + print_info "Updates will check daily at 2:00 AM" + print_info "Update logs: /var/log/mwc-update.log" + return 0 +} + # Main deployment flow main() { trap cleanup EXIT + # Handle --update-cron flag + if [[ "$1" == "--update-cron" ]]; then + print_header + setup_cron + exit $? + fi + print_header check_interactive check_root