Add support for adding and removing cron jobs

This commit is contained in:
Ben Peddell 2016-02-27 22:39:22 +10:00
parent 4eeeebeb8e
commit eb72148b35
2 changed files with 103 additions and 0 deletions

View File

@ -238,6 +238,37 @@ instances.
`status`::
Prints the status of the ARK server
`install-cronjob <command>`::
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=<arg>` option.
`--daily`;;
The command should be executed daily
`--hourly`;;
The command should be executed hourly
`--hour=<hour>`;;
Specifies one or more hours when the command should execute.
This is the hour field of the cron job.
`--minute=<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=<arg>`;;
Specifies an argument to pass to the command
`remove-cronjob <command>`::
Removes a cron job previously installed by `install-cronjob`
Configuration files
-------------------

View File

@ -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 <modid> Installs a mod from the Steam workshop"
echo "uninstallmod <modid> Removes the mod from the Mods directory"
echo "reinstallmod <modid> Removes and re-installs a mod in the Mods directory"
echo "install-cronjob <cmd> Adds a cron job using the specified command"
echo "remove-cronjob <cmd> 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