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
|
||||
# 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"
|
||||
|
||||
# Logging functions
|
||||
function log() {
|
||||
echo ":: $*"
|
||||
echo ":: $*" &>> "${JOBRUNNER_RUNDIR}/jobrunner.log"
|
||||
echo "${*}"
|
||||
echo "${*}" >> "/tmp/jobrunner/jobrunner.log"
|
||||
}
|
||||
function logcmd() {
|
||||
echo ":: EXEC \"$*\""
|
||||
$* &>> "${JOBRUNNER_RUNDIR}/jobrunner.log"
|
||||
log ":: Execute: ${*}"
|
||||
${*} &>> "/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
|
||||
if [ ! -d "${JOBRUNNER_RUNDIR}" ]; then
|
||||
logcmd mkdir -p "${JOBRUNNER_RUNDIR}"
|
||||
# Create jobrunner runtime directory if missing
|
||||
if [ ! -d "/tmp/jobrunner/" ]; then
|
||||
logcmd mkdir -p "/tmp/jobrunner/"
|
||||
echo "$$" > "/tmp/jobrunner/jobrunner.pid"
|
||||
fi
|
||||
|
||||
if [ -f "/tmp/jobrunner/jobrunner.cmd" ]; then
|
||||
log "Executing command"
|
||||
|
||||
# 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
|
||||
log "Exiting, as requested"
|
||||
logcmd rm -rf "/tmp/jobrunner/"
|
||||
exit 0
|
||||
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}"
|
||||
|
||||
# Execute command in subprocess
|
||||
bash -c "${JOBRUNNER_COMMAND}" &
|
||||
fi
|
||||
|
||||
# Sleep specified amount
|
||||
sleep "${JOBRUNNER_TIMEOUT}"
|
||||
done
|
||||
|
|
|
@ -1,22 +1,15 @@
|
|||
#!/bin/bash
|
||||
|
||||
if [ -f "${HOME}/.local/share/jobrunner.env" ]; then
|
||||
source "${HOME}/.local/share/jobrunner.env"
|
||||
fi
|
||||
|
||||
if [ -z "${*}" ]; then
|
||||
echo ":: Error: No command supplied"
|
||||
exit 1
|
||||
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."
|
||||
if [ -a "/tmp/jobrunner" ] && [ ! -d "/tmp/jobrunner" ]; then
|
||||
echo ":: Error: \"/tmp/jobrunner\" is not a directory, aborting."
|
||||
else
|
||||
if [ ! -f "${JOBRUNNER_RUNDIR}/jobrunner.pid" ]; then
|
||||
if [ ! -f "/tmp/jobrunner/jobrunner.pid" ]; then
|
||||
echo ":: Error: The jobrunner daemon is not running."
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
echo "${@}" &> "${JOBRUNNER_RUNDIR}/jobrunner.cmd"
|
||||
echo "${@}" &> "/tmp/jobrunner/jobrunner.cmd"
|
||||
|
|
Loading…
Reference in a new issue