|
| 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 |
0 commit comments