Update jobrunner daemon
This commit is contained in:
parent
da9b1db0ba
commit
f8f3203928
2 changed files with 61 additions and 57 deletions
|
@ -1,57 +1,68 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
# jeremystartm's jobrunner
|
# jeremystartm's jobrunner
|
||||||
|
|
||||||
if [ -f "${HOME}/.local/share/jobrunner.env" ]; then
|
# Logging functions
|
||||||
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() {
|
function log() {
|
||||||
echo ":: $*"
|
echo "${*}"
|
||||||
echo ":: $*" &>> "${JOBRUNNER_RUNDIR}/jobrunner.log"
|
echo "${*}" >> "/tmp/jobrunner/jobrunner.log"
|
||||||
}
|
}
|
||||||
function logcmd() {
|
function logcmd() {
|
||||||
echo ":: EXEC \"$*\""
|
log ":: Execute: ${*}"
|
||||||
$* &>> "${JOBRUNNER_RUNDIR}/jobrunner.log"
|
${*} &>> "/tmp/jobrunner/jobrunner.log"
|
||||||
}
|
}
|
||||||
|
|
||||||
log "Entering loop"
|
# Set default timeout
|
||||||
|
[[ -z "${JOBRUNNER_TIMEOUT}" ]] && export "JOBRUNNER_TIMEOUT=0.5s"
|
||||||
|
|
||||||
|
# Create runtime directory
|
||||||
|
if [ -a "/tmp/jobrunner/" ] && [ ! -d "/tmp/jobrunner/" ]; then
|
||||||
|
echo ":: Error: \"/tmp/jobrunner/\" is not a directory, aborting."
|
||||||
|
else
|
||||||
|
echo ":: Creating runtime directory"
|
||||||
|
mkdir -p "/tmp/jobrunner/"
|
||||||
|
echo "" > /tmp/jobrunner/jobrunner.{cmd,log}
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Check for PID file
|
||||||
|
if [ -f "/tmp/jobrunner/jobrunner.pid" ]; then
|
||||||
|
log ":: Error: The jobrunner daemon is already running."
|
||||||
|
log " If you think that this is an error, remove"
|
||||||
|
log " \"/tmp/jobrunner/jobrunner.pid\"."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Write PID file
|
||||||
|
log ":: Writing PID"
|
||||||
|
echo "$$" > "/tmp/jobrunner/jobrunner.pid"
|
||||||
|
|
||||||
|
log ":: Started jobrunner daemon"
|
||||||
|
|
||||||
|
# Enter loop
|
||||||
while true; do
|
while true; do
|
||||||
if [ ! -d "${JOBRUNNER_RUNDIR}" ]; then
|
# Create jobrunner runtime directory if missing
|
||||||
logcmd mkdir -p "${JOBRUNNER_RUNDIR}"
|
if [ ! -d "/tmp/jobrunner/" ]; then
|
||||||
|
logcmd mkdir -p "/tmp/jobrunner/"
|
||||||
|
echo "$$" > "/tmp/jobrunner/jobrunner.pid"
|
||||||
fi
|
fi
|
||||||
if [ -f "${JOBRUNNER_RUNDIR}/jobrunner.cmd" ]; then
|
|
||||||
|
if [ -f "/tmp/jobrunner/jobrunner.cmd" ]; then
|
||||||
log "Executing command"
|
log "Executing command"
|
||||||
JOBRUNNER_COMMAND="$(cat "${JOBRUNNER_RUNDIR}"/jobrunner.cmd)"
|
|
||||||
logcmd rm -rf "${JOBRUNNER_RUNDIR}/jobrunner.cmd"
|
# Read command file
|
||||||
|
JOBRUNNER_COMMAND="$(cat "/tmp/jobrunner/jobrunner.cmd")"
|
||||||
|
logcmd rm -rf "/tmp/jobrunner/jobrunner.cmd"
|
||||||
|
|
||||||
|
# Check if contains exit action
|
||||||
if [ "${JOBRUNNER_COMMAND}" == "EXIT" ]; then
|
if [ "${JOBRUNNER_COMMAND}" == "EXIT" ]; then
|
||||||
log "Exiting, as requested"
|
log "Exiting, as requested"
|
||||||
logcmd rm -rf "${JOBRUNNER_RUNDIR}"
|
logcmd rm -rf "/tmp/jobrunner/"
|
||||||
exit 1
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Execute command in subprocess
|
||||||
bash -c "${JOBRUNNER_COMMAND}" &
|
bash -c "${JOBRUNNER_COMMAND}" &
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Sleep specified amount
|
||||||
sleep "${JOBRUNNER_TIMEOUT}"
|
sleep "${JOBRUNNER_TIMEOUT}"
|
||||||
done
|
done
|
||||||
|
|
|
@ -1,22 +1,15 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
if [ -f "${HOME}/.local/share/jobrunner.env" ]; then
|
|
||||||
source "${HOME}/.local/share/jobrunner.env"
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ -z "${*}" ]; then
|
if [ -z "${*}" ]; then
|
||||||
echo ":: Error: No command supplied"
|
echo ":: Error: No command supplied"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
if [ -z "${JOBRUNNER_RUNDIR}" ]; then
|
if [ -a "/tmp/jobrunner" ] && [ ! -d "/tmp/jobrunner" ]; then
|
||||||
export "JOBRUNNER_RUNDIR=${XDG_RUNTIME_DIR}/jobrunner/"
|
echo ":: Error: \"/tmp/jobrunner\" is not a directory, aborting."
|
||||||
fi
|
|
||||||
if [ -a "${JOBRUNNER_RUNDIR}" ] && [ ! -d "${JOBRUNNER_RUNDIR}" ]; then
|
|
||||||
echo ":: Error: \"${JOBRUNNER_RUNDIR}\" is not a directory, aborting."
|
|
||||||
else
|
else
|
||||||
if [ ! -f "${JOBRUNNER_RUNDIR}/jobrunner.pid" ]; then
|
if [ ! -f "/tmp/jobrunner/jobrunner.pid" ]; then
|
||||||
echo ":: Error: The jobrunner daemon is not running."
|
echo ":: Error: The jobrunner daemon is not running."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
echo "${@}" &> "${JOBRUNNER_RUNDIR}/jobrunner.cmd"
|
echo "${@}" &> "/tmp/jobrunner/jobrunner.cmd"
|
||||||
|
|
Loading…
Reference in a new issue