Skip to content

Commit 4a1c3ee

Browse files
committed
feat(cron): add toggle-cronjobs command for automatically (un-)installing cronjobs
1 parent 9b62bde commit 4a1c3ee

File tree

4 files changed

+87
-1
lines changed

4 files changed

+87
-1
lines changed

lgsm/modules/check.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ if [ "$(whoami)" != "root" ]; then
4747
done
4848
fi
4949

50-
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)
50+
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)
5151
for allowed_command in "${allowed_commands_array[@]}"; do
5252
if [ "${allowed_command}" == "${commandname}" ]; then
5353
check_logs.sh
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/bin/bash
2+
# LinuxGSM command_toggle_cronjobs.sh module
3+
# Author: Daniel Gibbs
4+
# Contributors: http://linuxgsm.com/contrib
5+
# Website: https://linuxgsm.com
6+
# Description: Install and Uninstall cronjobs automatically.
7+
8+
commandname="TOGGLE-CRONJOBS"
9+
commandaction="Toggle cronjobs"
10+
moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
11+
fn_firstcommand_set
12+
13+
check.sh
14+
15+
# Identifier for automatically added cronjobs
16+
lgsmcomment="# added by LinuxGSM"
17+
18+
# Function to toggle a cronjob for the specified LinuxGSM command
19+
fn_toggle_cronjob() {
20+
local lgsmcommandname=$1
21+
local jobschedule=$2
22+
23+
local lgsmcommand="${rootdir}/${selfname} $lgsmcommandname"
24+
25+
# TODO: Decide wether to log cronjob output to "${lgsmlogdir}/${lgsmcommandname}-cronjob.log" by default
26+
local outputlog="/dev/null" # Replace with to log cron output
27+
local errorlog="/dev/null" # Replace with a log file path to log cron errors
28+
29+
local completejob="${jobschedule} ${lgsmcommand} > ${outputlog} 2> ${errorlog} ${lgsmcomment}"
30+
31+
local currentcrontab=$(crontab -l 2>/dev/null)
32+
33+
# If a cronjob for this LinuxGSM command already exists
34+
# ! ($| ) 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
35+
if echo "$currentcrontab" | grep -Eq "${lgsmcommand}($| )"; then
36+
# If the existing cronjob was auto-added by LinuxGSM
37+
if echo "$currentcrontab" | grep -E "${lgsmcommand}($| )" | grep -q "${lgsmcomment}"; then
38+
# Remove the existing cronjob
39+
local newcrontab=$(echo "$currentcrontab" | grep -Ev "${lgsmcommand}($| )")
40+
# Update the crontab to keep all cronjobs except the removed one
41+
echo "$newcrontab" | crontab -
42+
43+
# Check if the crontab was updated successfully
44+
if [ $? -eq 0 ]; then
45+
fn_print_ok_nl "Removed cronjob for '${lgsmcommand}'"
46+
fn_script_log_pass "Removed the auto-added cronjob for '${lgsmcommand}' from the crontab."
47+
else
48+
fn_print_fail_nl "Failed to remove cronjob for '${lgsmcommand}'"
49+
fn_script_log_fail "Failed to remove cronjob for '${lgsmcommand}' from the crontab."
50+
fi
51+
else
52+
# Job exists but was not auto-added by LinuxGSM, so skip
53+
fn_print_warn_nl "Cronjob for '${lgsmcommand}' already exists"
54+
fn_script_log_warn "A cronjob for '${lgsmcommand}' already exists but was not auto-added by LGSM."
55+
fi
56+
else
57+
# Add the job to the crontab while keeping existing cronjobs
58+
printf "%s\n%s\n" "$currentcrontab" "$completejob" | crontab -
59+
60+
# Check if the crontab was updated successfully
61+
if [ $? -eq 0 ]; then
62+
fn_print_ok_nl "Added the cronjob for '${lgsmcommand}'"
63+
fn_script_log_pass "Added the cronjob for '${lgsmcommand}' to the crontab."
64+
else
65+
fn_print_fail_nl "Failed to add cronjob for '${lgsmcommand}'"
66+
fn_script_log_fail "Failed to add the cronjob for '${lgsmcommand}' to the crontab."
67+
fi
68+
fi
69+
}
70+
71+
# Toggle cronjobs
72+
fn_toggle_cronjob "monitor" "*/5 * * * *" # Every 5 minutes
73+
fn_toggle_cronjob "update" "*/30 * * * *" # Every 30 minutes
74+
fn_toggle_cronjob "update-lgsm" "0 0 * * *" # Daily at midnight
75+
fn_toggle_cronjob "restart" "30 4 * * *" # Daily at 4:30am
76+
77+
core_exit.sh

lgsm/modules/core_getopt.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ cmd_restart=("r;restart" "command_restart.sh" "Restart the server.")
1818
cmd_details=("dt;details" "command_details.sh" "Display server information.")
1919
cmd_postdetails=("pd;postdetails" "command_postdetails.sh" "Post details to termbin.com (removing passwords).")
2020
cmd_backup=("b;backup" "command_backup.sh" "Create backup archives of the server.")
21+
cmd_toggle_cronjobs=("tc;toggle-cronjobs" "command_toggle_cronjobs.sh" "Install and Uninstall cronjobs automatically.")
2122
cmd_update_linuxgsm=("ul;update-lgsm;uf;update-modules" "command_update_linuxgsm.sh" "Check and apply any LinuxGSM updates.")
2223
cmd_test_alert=("ta;test-alert" "command_test_alert.sh" "Send a test alert.")
2324
cmd_monitor=("m;monitor" "command_monitor.sh" "Check server status and restart if crashed.")
@@ -84,6 +85,9 @@ fi
8485
# Backup.
8586
currentopt+=("${cmd_backup[@]}")
8687

88+
# Install Cronjobs
89+
currentopt+=("${cmd_toggle_cronjobs[@]}")
90+
8791
# Console & Debug.
8892
currentopt+=("${cmd_console[@]}" "${cmd_debug[@]}")
8993

lgsm/modules/core_modules.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ command_backup.sh() {
7171
fn_fetch_module
7272
}
7373

74+
command_toggle_cronjobs.sh() {
75+
modulefile="${FUNCNAME[0]}"
76+
fn_fetch_module
77+
}
78+
7479
command_console.sh() {
7580
modulefile="${FUNCNAME[0]}"
7681
fn_fetch_module

0 commit comments

Comments
 (0)