From 4a1c3eea2320d5a8809167b0d262644a2ce15d90 Mon Sep 17 00:00:00 2001 From: MicLieg Date: Tue, 13 Feb 2024 01:26:35 +0100 Subject: [PATCH] feat(cron): add toggle-cronjobs command for automatically (un-)installing cronjobs --- lgsm/modules/check.sh | 2 +- lgsm/modules/command_toggle_cronjobs.sh | 77 +++++++++++++++++++++++++ lgsm/modules/core_getopt.sh | 4 ++ lgsm/modules/core_modules.sh | 5 ++ 4 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 lgsm/modules/command_toggle_cronjobs.sh diff --git a/lgsm/modules/check.sh b/lgsm/modules/check.sh index 2a63df3f3e..3d67d758da 100644 --- a/lgsm/modules/check.sh +++ b/lgsm/modules/check.sh @@ -47,7 +47,7 @@ if [ "$(whoami)" != "root" ]; then done fi -allowed_commands_array=(BACKUP CONSOLE DEBUG DETAILS MAP-COMPRESSOR FASTDL MODS-INSTALL MODS-REMOVE MODS-UPDATE MONITOR POST-DETAILS RESTART START STOP TEST-ALERT CHANGE-PASSWORD UPDATE UPDATE-LGSM VALIDATE WIPE) +allowed_commands_array=(BACKUP CONSOLE DEBUG DETAILS MAP-COMPRESSOR FASTDL MODS-INSTALL MODS-REMOVE MODS-UPDATE MONITOR POST-DETAILS RESTART START STOP TEST-ALERT CHANGE-PASSWORD UPDATE UPDATE-LGSM VALIDATE WIPE TOGGLE-CRONJOBS) for allowed_command in "${allowed_commands_array[@]}"; do if [ "${allowed_command}" == "${commandname}" ]; then check_logs.sh diff --git a/lgsm/modules/command_toggle_cronjobs.sh b/lgsm/modules/command_toggle_cronjobs.sh new file mode 100644 index 0000000000..e727b03984 --- /dev/null +++ b/lgsm/modules/command_toggle_cronjobs.sh @@ -0,0 +1,77 @@ +#!/bin/bash +# LinuxGSM command_toggle_cronjobs.sh module +# Author: Daniel Gibbs +# Contributors: http://linuxgsm.com/contrib +# Website: https://linuxgsm.com +# Description: Install and Uninstall cronjobs automatically. + +commandname="TOGGLE-CRONJOBS" +commandaction="Toggle cronjobs" +moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")" +fn_firstcommand_set + +check.sh + +# Identifier for automatically added cronjobs +lgsmcomment="# added by LinuxGSM" + +# Function to toggle a cronjob for the specified LinuxGSM command +fn_toggle_cronjob() { + local lgsmcommandname=$1 + local jobschedule=$2 + + local lgsmcommand="${rootdir}/${selfname} $lgsmcommandname" + + # TODO: Decide wether to log cronjob output to "${lgsmlogdir}/${lgsmcommandname}-cronjob.log" by default + local outputlog="/dev/null" # Replace with to log cron output + local errorlog="/dev/null" # Replace with a log file path to log cron errors + + local completejob="${jobschedule} ${lgsmcommand} > ${outputlog} 2> ${errorlog} ${lgsmcomment}" + + local currentcrontab=$(crontab -l 2>/dev/null) + + # If a cronjob for this LinuxGSM command already exists + # ! ($| ) is used to match the end of the line or a space after the command to avoid matching similar commands like ./gameserver update & ./gameserver update-lgsm + if echo "$currentcrontab" | grep -Eq "${lgsmcommand}($| )"; then + # If the existing cronjob was auto-added by LinuxGSM + if echo "$currentcrontab" | grep -E "${lgsmcommand}($| )" | grep -q "${lgsmcomment}"; then + # Remove the existing cronjob + local newcrontab=$(echo "$currentcrontab" | grep -Ev "${lgsmcommand}($| )") + # Update the crontab to keep all cronjobs except the removed one + echo "$newcrontab" | crontab - + + # Check if the crontab was updated successfully + if [ $? -eq 0 ]; then + fn_print_ok_nl "Removed cronjob for '${lgsmcommand}'" + fn_script_log_pass "Removed the auto-added cronjob for '${lgsmcommand}' from the crontab." + else + fn_print_fail_nl "Failed to remove cronjob for '${lgsmcommand}'" + fn_script_log_fail "Failed to remove cronjob for '${lgsmcommand}' from the crontab." + fi + else + # Job exists but was not auto-added by LinuxGSM, so skip + fn_print_warn_nl "Cronjob for '${lgsmcommand}' already exists" + fn_script_log_warn "A cronjob for '${lgsmcommand}' already exists but was not auto-added by LGSM." + fi + else + # Add the job to the crontab while keeping existing cronjobs + printf "%s\n%s\n" "$currentcrontab" "$completejob" | crontab - + + # Check if the crontab was updated successfully + if [ $? -eq 0 ]; then + fn_print_ok_nl "Added the cronjob for '${lgsmcommand}'" + fn_script_log_pass "Added the cronjob for '${lgsmcommand}' to the crontab." + else + fn_print_fail_nl "Failed to add cronjob for '${lgsmcommand}'" + fn_script_log_fail "Failed to add the cronjob for '${lgsmcommand}' to the crontab." + fi + fi +} + +# Toggle cronjobs +fn_toggle_cronjob "monitor" "*/5 * * * *" # Every 5 minutes +fn_toggle_cronjob "update" "*/30 * * * *" # Every 30 minutes +fn_toggle_cronjob "update-lgsm" "0 0 * * *" # Daily at midnight +fn_toggle_cronjob "restart" "30 4 * * *" # Daily at 4:30am + +core_exit.sh diff --git a/lgsm/modules/core_getopt.sh b/lgsm/modules/core_getopt.sh index cd3e57cb57..369436e31b 100644 --- a/lgsm/modules/core_getopt.sh +++ b/lgsm/modules/core_getopt.sh @@ -18,6 +18,7 @@ cmd_restart=("r;restart" "command_restart.sh" "Restart the server.") cmd_details=("dt;details" "command_details.sh" "Display server information.") cmd_postdetails=("pd;postdetails" "command_postdetails.sh" "Post details to termbin.com (removing passwords).") cmd_backup=("b;backup" "command_backup.sh" "Create backup archives of the server.") +cmd_toggle_cronjobs=("tc;toggle-cronjobs" "command_toggle_cronjobs.sh" "Install and Uninstall cronjobs automatically.") cmd_update_linuxgsm=("ul;update-lgsm;uf;update-modules" "command_update_linuxgsm.sh" "Check and apply any LinuxGSM updates.") cmd_test_alert=("ta;test-alert" "command_test_alert.sh" "Send a test alert.") cmd_monitor=("m;monitor" "command_monitor.sh" "Check server status and restart if crashed.") @@ -84,6 +85,9 @@ fi # Backup. currentopt+=("${cmd_backup[@]}") +# Install Cronjobs +currentopt+=("${cmd_toggle_cronjobs[@]}") + # Console & Debug. currentopt+=("${cmd_console[@]}" "${cmd_debug[@]}") diff --git a/lgsm/modules/core_modules.sh b/lgsm/modules/core_modules.sh index 7ea0402e35..d054de5476 100644 --- a/lgsm/modules/core_modules.sh +++ b/lgsm/modules/core_modules.sh @@ -71,6 +71,11 @@ command_backup.sh() { fn_fetch_module } +command_toggle_cronjobs.sh() { + modulefile="${FUNCNAME[0]}" + fn_fetch_module +} + command_console.sh() { modulefile="${FUNCNAME[0]}" fn_fetch_module