@@ -32,6 +32,7 @@ LOCUST_FILE="$EXTRACT_DIR/SystemGuard-*/src/scripts/locustfile.py"
3232HOST_URL=" http://localhost:5050"
3333INSTALLER_SCRIPT=' setup.sh'
3434ISSUE_URL=" https://github.com/codeperfectplus/SystemGuard/issues"
35+ CRON_PATTERN=" .systemguard/SystemGuard-.*/src/scripts/dashboard.sh"
3536
3637# Create necessary directories
3738mkdir -p " $LOG_DIR "
@@ -43,6 +44,14 @@ log() {
4344 echo " $( date ' +%Y-%m-%d %H:%M:%S' ) - $message " | tee -a " $LOG_FILE "
4445}
4546
47+ # Check if running with sudo
48+ if [ " $EUID " -eq 0 ]; then
49+ crontab_cmd=" crontab -u $USER_NAME "
50+ else
51+ crontab_cmd=" crontab"
52+ fi
53+
54+
4655# Function to add a cron job with error handling
4756add_cron_job () {
4857
@@ -58,14 +67,6 @@ add_cron_job() {
5867 exit 1
5968 fi
6069
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"
68- fi
6970
7071 # Temporarily store current crontab to avoid overwriting on error
7172 local temp_cron=$( mktemp)
@@ -75,7 +76,7 @@ add_cron_job() {
7576 fi
7677
7778 # List the current crontab
78- if ! crontab -l 2> /dev/null > " $temp_cron " ; then
79+ if ! $crontab_cmd -l 2> /dev/null > " $temp_cron " ; then
7980 log " Error: Unable to list current crontab."
8081 rm " $temp_cron "
8182 exit 1
@@ -215,9 +216,8 @@ install() {
215216
216217 # Clean up previous cron jobs related to SystemGuard
217218 log " Cleaning up previous cron jobs related to SystemGuard..."
218- CRON_PATTERN=" .systemguard/SystemGuard-.*/src/scripts/dashboard.sh"
219- if crontab -l | grep -q " $CRON_PATTERN " ; then
220- crontab -l | grep -v " $CRON_PATTERN " | crontab -
219+ if $crontab_cmd -l | grep -q " $CRON_PATTERN " ; then
220+ $crontab_cmd -l | grep -v " $CRON_PATTERN " | $crontab_cmd -
221221 log " Old cron jobs removed."
222222 else
223223 log " No previous cron jobs found."
@@ -235,6 +235,14 @@ install() {
235235 log " Preparing cronjob script..."
236236 add_cron_job
237237
238+ # check if the cron job is added successfully
239+ if $crontab_cmd -l | grep -q " $CRON_PATTERN " ; then
240+ log " Cron job added successfully."
241+ else
242+ log " Error: Failed to add the cron job."
243+ exit 1
244+ fi
245+
238246 # Install the executable
239247 install_executable
240248 log " SystemGuard version $VERSION installed successfully!"
@@ -245,15 +253,19 @@ uninstall() {
245253 log " Uninstalling SystemGuard..."
246254
247255 # Remove cron jobs related to SystemGuard
248- CRON_PATTERN= " \.systemguard/SystemGuard-.*\/dashboard\.sh "
256+ # cronjob
249257
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 -
258+ if $crontab_cmd -l 2> /dev/null | grep -E " $CRON_PATTERN " > /dev/null; then
259+ echo " Are you sure you want to remove all cron jobs related to SystemGuard? (y/n)"
260+ read CONFIRM
261+ if [ " $CONFIRM " != " y" ]; then
262+ log " Uninstallation aborted by user."
263+ exit 0
264+ fi
265+ $crontab_cmd -l | grep -v " $CRON_PATTERN " | $crontab_cmd -
254266 log " Cron jobs removed."
255267 else
256- log " No cron jobs found matching the pattern ."
268+ log " No cron jobs found."
257269 fi
258270
259271 # Remove the SystemGuard installation directory
@@ -301,8 +313,8 @@ check_status() {
301313 log " SystemGuard is not installed."
302314 fi
303315
304- CRON_PATTERN= " .systemguard/SystemGuard-.*/src/scripts/dashboard.sh "
305- if crontab -l | grep -q " $CRON_PATTERN " ; then
316+
317+ if $crontab_cmd ' -l ' | grep -q " $CRON_PATTERN " ; then
306318 log " Cron job for SystemGuard is set."
307319 else
308320 log " No cron job found for SystemGuard."
0 commit comments