From eb72148b35a319c434e491386c3c3c435750656d Mon Sep 17 00:00:00 2001 From: Ben Peddell Date: Sat, 27 Feb 2016 22:39:22 +1000 Subject: [PATCH] Add support for adding and removing cron jobs --- README.asciidoc | 31 +++++++++++++++++++++ tools/arkmanager | 72 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) diff --git a/README.asciidoc b/README.asciidoc index 31abc77..2f6a39d 100644 --- a/README.asciidoc +++ b/README.asciidoc @@ -238,6 +238,37 @@ instances. `status`:: Prints the status of the ARK server +`install-cronjob `:: + Installs a cron job that executes the specified command. + This accepts any of the options the specified command accepts, + as well as the following options. In order to specify an + argument to the command (e.g. to the `broadcast` command), + use the `--arg=` option. + + `--daily`;; + The command should be executed daily + + `--hourly`;; + The command should be executed hourly + + `--hour=`;; + Specifies one or more hours when the command should execute. + This is the hour field of the cron job. + + `--minute=`;; + Specifies one or more minutes of the hour when the command + should execute. This is the minute field of the cron job. + + `--enable-output`;; + Enables the output from the command - the cron daemon usually + emails this to the user specified in the cron configuration + + `--arg=`;; + Specifies an argument to pass to the command + +`remove-cronjob `:: + Removes a cron job previously installed by `install-cronjob` + Configuration files ------------------- diff --git a/tools/arkmanager b/tools/arkmanager index b6f8201..37ecdae 100755 --- a/tools/arkmanager +++ b/tools/arkmanager @@ -1561,6 +1561,64 @@ doBackup(){ fi } +# +# Install a cron job to execute a particular command +# +doInstallCronJob(){ + hour='*' + minute='0' + cmdopts="${arkCronExtraOpts}" + cmdargs="" + output=">/dev/null 2>&1" + command="$1" + shift + + for opt in "$@"; do + case "$opt" in + --daily) + ;; + --hourly) + hour='*' + ;; + --hour=*) + hour="${opt#--hour=}" + ;; + --minute=*) + minute="${opt#--minute=}" + ;; + --enable-output) + output= + ;; + --arg=*) + cmdargs="${cmdargs} $(printf "%q" "${opt#--opt=}")" + ;; + --*) + cmdopts="${cmdopts} $(printf "%q" "${opt}")" + ;; + *) + cmdargs="${args} $(printf "%q" "${opt}")" + ;; + esac + done + + cronjob="${minute} ${hour} * * * arkmanager --cronjob ${command} @${instance} ${cmdopts} --args ${cmdargs} -- ${output}" + + (crontab -l | \ + sed -e "/ [*] [*] [*] arkmanager --cronjob ${command} @${instance} /d"; + echo "${cronjob}" ) | \ + crontab - +} + +# +# Removes an installed cron job +# +doRemoveCronJob(){ + command="$1" + + crontab -l | \ + sed -e "/ [*] [*] [*] arkmanager --cronjob ${command} @${instance} /d" | \ + crontab - +} # # Print the status of the server (running? online? version?) @@ -1697,6 +1755,8 @@ showUsage() { echo "installmod Installs a mod from the Steam workshop" echo "uninstallmod Removes the mod from the Mods directory" echo "reinstallmod Removes and re-installs a mod in the Mods directory" + echo "install-cronjob Adds a cron job using the specified command" + echo "remove-cronjob Removes a cron job that used the specified command" echo "restart Stops the server and then starts it" echo "run Runs the server without daemonizing" echo "start Starts the server" @@ -1746,6 +1806,10 @@ while true; do progressDisplayType=spinner continue ;; + --cronjob) + inCronJob=true + continue + ;; esac # get the number of arguments for commands that take arguments @@ -1756,6 +1820,8 @@ while true; do broadcast) nrarg=1; ;; rconcmd) nrarg=1; ;; useconfig) nrarg=1; ;; + install-cronjob) nrarg=1; ;; + remove-cronjob) nrarg=1; ;; esac # Enumerate the options and arguments @@ -1912,6 +1978,12 @@ while true; do status) printStatus ;; + install-cronjob) + doInstallCronJob "${args[@]}" "${options[@]}" + ;; + remove-cronjob) + doRemoveCronJob "${args[@]}" + ;; *) echo -n "arkmanager v${arkstVersion}: unknown command '$command' specified" showUsage