From 3b727943078f4dd5fe55b230f5a10ea1ff991dea Mon Sep 17 00:00:00 2001 From: Muhammad Ibrahim Date: Mon, 29 Sep 2025 11:59:49 +0100 Subject: [PATCH] made the crontab installation safe as opposed to nuking it - spotted by thespad --- agents/patchmon_install.sh | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/agents/patchmon_install.sh b/agents/patchmon_install.sh index 7c30d6a..f9c8848 100644 --- a/agents/patchmon_install.sh +++ b/agents/patchmon_install.sh @@ -160,12 +160,22 @@ fi # Setup crontab for automatic package status updates info "⏰ Setting up automatic package status update every $UPDATE_INTERVAL minutes..." + +# Check if patchmon cron job already exists +PATCHMON_CRON_EXISTS=$(crontab -l 2>/dev/null | grep -c "patchmon-agent.sh update" || true) + +if [[ $PATCHMON_CRON_EXISTS -gt 0 ]]; then + info " Existing PatchMon cron job found, removing old entry..." + # Remove existing patchmon cron entries and preserve other entries + (crontab -l 2>/dev/null | grep -v "patchmon-agent.sh update") | crontab - +fi + if [[ $UPDATE_INTERVAL -eq 60 ]]; then - # Hourly updates - echo "0 * * * * /usr/local/bin/patchmon-agent.sh update >/dev/null 2>&1" | crontab - + # Hourly updates - safely append to existing crontab + (crontab -l 2>/dev/null; echo "0 * * * * /usr/local/bin/patchmon-agent.sh update >/dev/null 2>&1") | crontab - else - # Custom interval updates - echo "*/$UPDATE_INTERVAL * * * * /usr/local/bin/patchmon-agent.sh update >/dev/null 2>&1" | crontab - + # Custom interval updates - safely append to existing crontab + (crontab -l 2>/dev/null; echo "*/$UPDATE_INTERVAL * * * * /usr/local/bin/patchmon-agent.sh update >/dev/null 2>&1") | crontab - fi success "🎉 PatchMon Agent installation complete!"