Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cron): add toggle-cronjobs command #4508

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lgsm/modules/check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
76 changes: 76 additions & 0 deletions lgsm/modules/command_toggle_cronjobs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/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
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
newcrontab=$(echo "$currentcrontab" | grep -Ev "${lgsmcommand}($| )")

# Update the crontab to keep all cronjobs except the removed one
# Check if the crontab was updated successfully
if echo "$newcrontab" | crontab -; 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
# Check if the crontab was updated successfully
if printf "%s\n%s\n" "$currentcrontab" "$completejob" | crontab -; 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
4 changes: 4 additions & 0 deletions lgsm/modules/core_getopt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down Expand Up @@ -84,6 +85,9 @@ fi
# Backup.
currentopt+=("${cmd_backup[@]}")

# Install Cronjobs
currentopt+=("${cmd_toggle_cronjobs[@]}")

# Console & Debug.
currentopt+=("${cmd_console[@]}" "${cmd_debug[@]}")

Expand Down
5 changes: 5 additions & 0 deletions lgsm/modules/core_modules.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down