From 7bcfe3d671092ff00f273dbe7e672855e4c93a00 Mon Sep 17 00:00:00 2001 From: Arpit Joshi Date: Thu, 12 Sep 2024 18:00:03 +0530 Subject: [PATCH] Empty crontab issue resolved --- setup.sh | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/setup.sh b/setup.sh index e43d738..3ccb5bc 100644 --- a/setup.sh +++ b/setup.sh @@ -314,10 +314,17 @@ add_cron_job() { fi # List the current crontab - if ! $crontab_cmd -l 2>/dev/null > "$temp_cron"; then - log "CRITICAL" "Unable to list current crontab." - rm "$temp_cron" - exit 1 + if ! $crontab_cmd -l > "$temp_cron" 2>&1; then + # Handle empty cron file error + if grep -q "no crontab for" "$temp_cron" 2>/dev/null; then + grep -v "no crontab for" "$temp_cron" > "${temp_cron}.clean" + mv "${temp_cron}.clean" "$temp_cron" + log "No crontab for user $USER_NAME." + else + log "CRITICAL" "Unable to list current crontab. hello" + rm "$temp_cron" + exit 1 + fi fi # Ensure the cron job does not already exist