@@ -45,44 +45,59 @@ log() {
4545
4646# Function to add a cron job with error handling
4747add_cron_job () {
48+
4849 # Define log directory and cron job command
49- local USER_NAME= $( whoami )
50- local log_dir= " /home/ $USER_NAME /logs "
51- local cron_job=" * * * * * /bin/bash $( find $EXTRACT_DIR -name dashboard.sh ) >> $log_dir /systemguard_cron.log 2>&1"
50+ local log_dir= " $USER_HOME /logs "
51+ local script_path= $( find " $EXTRACT_DIR " -name dashboard.sh )
52+ local cron_job=" * * * * * /bin/bash $script_path >> $log_dir /systemguard_cron.log 2>&1"
5253
5354 # Create log directory with error handling
5455 mkdir -p " $log_dir "
5556 if [ $? -ne 0 ]; then
56- echo " Error: Failed to create log directory: $log_dir "
57+ log " Error: Failed to create log directory: $log_dir "
5758 exit 1
5859 fi
5960
60- # Verify user retrieval
61- if [ -z " $USER_NAME " ]; then
62- echo " Error: Unable to retrieve current username."
63- exit 1
61+ # Check if running with sudo
62+ if [ " $EUID " -eq 0 ]; then
63+ # Running as root, modify the crontab for the target user
64+ local crontab_cmd=" crontab -u $USER_NAME "
65+ else
66+ # Running as a regular user, modify current user's crontab
67+ local crontab_cmd=" crontab"
6468 fi
6569
66- # Add cron job to crontab with error handling
6770 # Temporarily store current crontab to avoid overwriting on error
68- temp_cron=$( mktemp)
69- crontab -l 2> /dev/null > " $temp_cron "
70- if [ $? -ne 0 ] && [ ! -s " $temp_cron " ]; then
71- echo " Error: Unable to list current crontab or it's empty."
71+ local temp_cron=$( mktemp)
72+ if [ $? -ne 0 ]; then
73+ log " Error: Failed to create temporary file for crontab."
74+ exit 1
75+ fi
76+
77+ # List the current crontab
78+ if ! crontab -l 2> /dev/null > " $temp_cron " ; then
79+ log " Error: Unable to list current crontab."
7280 rm " $temp_cron "
7381 exit 1
7482 fi
75-
83+
84+ # Ensure the cron job does not already exist
85+ if grep -Fxq " $cron_job " " $temp_cron " ; then
86+ log " Cron job already exists: $cron_job "
87+ rm " $temp_cron "
88+ exit 0
89+ fi
90+
91+ # Add the new cron job
7692 echo " $cron_job " >> " $temp_cron "
77- crontab " $temp_cron "
78- if [ $? -ne 0 ]; then
79- echo " Error: Failed to add the cron job to the crontab."
93+ if ! $crontab_cmd " $temp_cron " ; then
94+ log " Error: Failed to add the cron job to crontab."
8095 rm " $temp_cron "
8196 exit 1
8297 fi
8398
8499 rm " $temp_cron "
85- echo " Cron job added successfully: $cron_job "
100+ log " Cron job added successfully: $cron_job "
86101}
87102
88103# Backup function for existing configurations
@@ -217,15 +232,8 @@ install() {
217232 unzip -q $DOWNLOAD_DIR /systemguard.zip -d $EXTRACT_DIR
218233 rm $DOWNLOAD_DIR /systemguard.zip
219234 log " Extraction completed."
220-
221235 log " Preparing cronjob script..."
222236 add_cron_job
223- # check if the cronjob added successfully or not
224- if ! crontab -l | grep -q " $CRON_PATTERN " ; then
225- log " Error: Failed to add the cron job to the crontab."
226- log " Installation failed... Report this issue to the SystemGuard maintainers at $ISSUE_URL ."
227- exit 1
228- fi
229237
230238 # Install the executable
231239 install_executable
@@ -237,13 +245,15 @@ uninstall() {
237245 log " Uninstalling SystemGuard..."
238246
239247 # Remove cron jobs related to SystemGuard
240- log " Cleaning up cron jobs related to SystemGuard..."
241- CRON_PATTERN=" .systemguard/SystemGuard-.*/dashboard.sh"
242- if crontab -l | grep -q " $CRON_PATTERN " ; then
243- crontab -l | grep -v " $CRON_PATTERN " | crontab -
248+ CRON_PATTERN=" \.systemguard/SystemGuard-.*\/dashboard\.sh"
249+
250+ # List current crontab entries and filter out the pattern
251+ if crontab -l | grep -E -q " $CRON_PATTERN " ; then
252+ # Remove matching cron jobs
253+ crontab -l | grep -E -v " $CRON_PATTERN " | crontab -
244254 log " Cron jobs removed."
245255 else
246- log " No cron jobs found."
256+ log " No cron jobs found matching the pattern ."
247257 fi
248258
249259 # Remove the SystemGuard installation directory
0 commit comments