@@ -23,14 +23,15 @@ get_user_home() {
2323USER_HOME=$( get_user_home)
2424USER_NAME=$( echo $USER_HOME | awk -F' /' ' {print $3}' )
2525DOWNLOAD_DIR=" /tmp"
26- EXTRACT_DIR=" $USER_HOME /.systemguard/ "
26+ EXTRACT_DIR=" $USER_HOME /.systemguard"
2727LOG_DIR=" $HOME /logs"
2828LOG_FILE=" $LOG_DIR /systemguard-installer.log"
2929BACKUP_DIR=" $USER_HOME /.systemguard_backup"
3030EXECUTABLE=" /usr/local/bin/systemguard-installer"
3131LOCUST_FILE=" $EXTRACT_DIR /SystemGuard-*/src/scripts/locustfile.py"
3232HOST_URL=" http://localhost:5050"
3333INSTALLER_SCRIPT=' setup.sh'
34+ ISSUE_URL=" https://github.com/codeperfectplus/SystemGuard/issues"
3435
3536# Create necessary directories
3637mkdir -p " $LOG_DIR "
@@ -42,6 +43,48 @@ log() {
4243 echo " $( date ' +%Y-%m-%d %H:%M:%S' ) - $message " | tee -a " $LOG_FILE "
4344}
4445
46+ # Function to add a cron job with error handling
47+ add_cron_job () {
48+ # 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"
52+
53+ # Create log directory with error handling
54+ mkdir -p " $log_dir "
55+ if [ $? -ne 0 ]; then
56+ echo " Error: Failed to create log directory: $log_dir "
57+ exit 1
58+ fi
59+
60+ # Verify user retrieval
61+ if [ -z " $USER_NAME " ]; then
62+ echo " Error: Unable to retrieve current username."
63+ exit 1
64+ fi
65+
66+ # Add cron job to crontab with error handling
67+ # 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."
72+ rm " $temp_cron "
73+ exit 1
74+ fi
75+
76+ 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."
80+ rm " $temp_cron "
81+ exit 1
82+ fi
83+
84+ rm " $temp_cron "
85+ echo " Cron job added successfully: $cron_job "
86+ }
87+
4588# Backup function for existing configurations
4689backup_configs () {
4790 log " Backing up existing configurations..."
@@ -157,7 +200,7 @@ install() {
157200
158201 # Clean up previous cron jobs related to SystemGuard
159202 log " Cleaning up previous cron jobs related to SystemGuard..."
160- CRON_PATTERN=" .systemguard/SystemGuard-.*/dashboard.sh"
203+ CRON_PATTERN=" .systemguard/SystemGuard-.*/src/scripts/ dashboard.sh"
161204 if crontab -l | grep -q " $CRON_PATTERN " ; then
162205 crontab -l | grep -v " $CRON_PATTERN " | crontab -
163206 log " Old cron jobs removed."
@@ -175,20 +218,15 @@ install() {
175218 rm $DOWNLOAD_DIR /systemguard.zip
176219 log " Extraction completed."
177220
178- # Navigate to the setup directory and execute setup script
179- log " Navigating to the SystemGuard setup directory..."
180- cd $EXTRACT_DIR /SystemGuard-* /src/scripts
181- if [ ! -f " cronjob.sh" ]; then
182- log " Error: cronjob.sh not found in the extracted directory. Please verify the contents."
221+ log " Preparing cronjob script..."
222+ 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 ."
183227 exit 1
184228 fi
185229
186- # Make cronjob.sh executable and run it
187- log " Preparing cronjob script..."
188- chmod +x cronjob.sh
189- log " Executing the cronjob setup..."
190- ./cronjob.sh
191-
192230 # Install the executable
193231 install_executable
194232 log " SystemGuard version $VERSION installed successfully!"
0 commit comments