Update jobrunner daemon

This commit is contained in:
JeremyStar™ 2024-06-03 10:01:06 +02:00
parent da9b1db0ba
commit f8f3203928
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D
2 changed files with 61 additions and 57 deletions

View file

@ -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
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 fi
if [ -f "${JOBRUNNER_RUNDIR}/jobrunner.cmd" ]; then
log "Executing command" # Execute command in subprocess
JOBRUNNER_COMMAND="$(cat "${JOBRUNNER_RUNDIR}"/jobrunner.cmd)" bash -c "${JOBRUNNER_COMMAND}" &
logcmd rm -rf "${JOBRUNNER_RUNDIR}/jobrunner.cmd" fi
if [ "${JOBRUNNER_COMMAND}" == "EXIT" ]; then
log "Exiting, as requested" # Sleep specified amount
logcmd rm -rf "${JOBRUNNER_RUNDIR}" sleep "${JOBRUNNER_TIMEOUT}"
exit 1
fi
bash -c "${JOBRUNNER_COMMAND}" &
fi
sleep "${JOBRUNNER_TIMEOUT}"
done done

View file

@ -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"