punktdateien/jobrunner/daemon.sh

58 lines
1.6 KiB
Bash
Raw Normal View History

2023-12-08 17:33:11 +01:00
#!/bin/bash
# jeremystartm's jobrunner
if [ -f "${HOME}/.local/share/jobrunner.env" ]; then
source "${HOME}/.local/share/jobrunner.env"
fi
if [ -z "${JOBRUNNER_TIMEOUT}" ]; then
export "JOBRUNNER_TIMEOUT=0.5s"
fi
if [ -z "${JOBRUNNER_RUNDIR}" ]; then
export "JOBRUNNER_RUNDIR=${XDG_RUNTIME_DIR}/jobrunner/"
fi
if [ -a "${JOBRUNNER_RUNDIR}" ] && [ ! -d "${JOBRUNNER_RUNDIR}" ]; then
echo ":: Error: \"${JOBRUNNER_RUNDIR}\" is not a directory, aborting."
else
echo ":: Creating RUNDIR"
mkdir -p "${JOBRUNNER_RUNDIR}"
fi
if [ -f "${JOBRUNNER_RUNDIR}/jobrunner.pid" ]; then
echo ":: Error: The jobrunner daemon is already running."
echo " If you think that this is an error, remove"
echo " \"${JOBRUNNER_RUNDIR}/jobrunner.pid\"."
exit 1
fi
echo ":: Writing PID"
echo "$$" > "${JOBRUNNER_RUNDIR}/jobrunner.pid"
echo ":: Started jobrunner daemon" &> "${JOBRUNNER_RUNDIR}/jobrunner.log"
function log() {
echo ":: $*"
echo ":: $*" &>> "${JOBRUNNER_RUNDIR}/jobrunner.log"
}
function logcmd() {
echo ":: EXEC \"$*\""
$* &>> "${JOBRUNNER_RUNDIR}/jobrunner.log"
}
log "Entering loop"
while true; do
if [ ! -d "${JOBRUNNER_RUNDIR}" ]; then
logcmd mkdir -p "${JOBRUNNER_RUNDIR}"
fi
if [ -f "${JOBRUNNER_RUNDIR}/jobrunner.cmd" ]; then
log "Executing command"
JOBRUNNER_COMMAND="$(cat "${JOBRUNNER_RUNDIR}"/jobrunner.cmd)"
logcmd rm -rf "${JOBRUNNER_RUNDIR}/jobrunner.cmd"
if [ "${JOBRUNNER_COMMAND}" == "EXIT" ]; then
log "Exiting, as requested"
logcmd rm -rf "${JOBRUNNER_RUNDIR}"
exit 1
fi
bash -c "${JOBRUNNER_COMMAND}" &
fi
sleep "${JOBRUNNER_TIMEOUT}"
done