From f8f3203928732e7ffef16a9035b52dfa70c0fad8 Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Mon, 3 Jun 2024 10:01:06 +0200 Subject: [PATCH] Update jobrunner daemon --- bash/jobrunner/daemon.sh | 103 ++++++++++++++++++++++----------------- bash/jobrunner/runjob.sh | 15 ++---- 2 files changed, 61 insertions(+), 57 deletions(-) diff --git a/bash/jobrunner/daemon.sh b/bash/jobrunner/daemon.sh index e46e395b..f5b5c86b 100755 --- a/bash/jobrunner/daemon.sh +++ b/bash/jobrunner/daemon.sh @@ -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 diff --git a/bash/jobrunner/runjob.sh b/bash/jobrunner/runjob.sh index d9262acb..4a7242e9 100755 --- a/bash/jobrunner/runjob.sh +++ b/bash/jobrunner/runjob.sh @@ -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"