punktdateien/bash/jobrunner/daemon.sh

72 lines
1.8 KiB
Bash
Raw Normal View History

2023-12-08 17:33:11 +01:00
#!/bin/bash
# jeremystartm's jobrunner
2024-06-03 10:01:06 +02:00
# Logging functions
function log() {
2024-06-03 10:29:56 +02:00
[[ -z "${JOBRUNNER_DISABLEOUTPUT}" ]] && echo "${*}"
2024-06-03 10:01:06 +02:00
echo "${*}" >> "/tmp/jobrunner/jobrunner.log"
}
function logcmd() {
log ":: Execute: ${*}"
${*} &>> "/tmp/jobrunner/jobrunner.log"
}
2023-12-08 17:33:11 +01:00
2024-06-03 10:01:06 +02:00
# Set default timeout
2024-06-03 10:29:56 +02:00
[[ -z "${JOBRUNNER_TIMEOUT}" ]] && export "JOBRUNNER_TIMEOUT=0.1s"
2024-06-03 10:01:06 +02:00
# Create runtime directory
if [ -a "/tmp/jobrunner/" ] && [ ! -d "/tmp/jobrunner/" ]; then
echo ":: Error: \"/tmp/jobrunner/\" is not a directory, aborting."
2023-12-08 17:33:11 +01:00
else
2024-06-03 10:01:06 +02:00
echo ":: Creating runtime directory"
mkdir -p "/tmp/jobrunner/"
2024-06-03 10:29:56 +02:00
echo -e "" > /tmp/jobrunner/jobrunner.log
rm -rf /tmp/jobrunner/jobrunner.cmd
2023-12-08 17:33:11 +01:00
fi
2024-06-03 10:01:06 +02:00
# 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
2023-12-08 17:33:11 +01:00
fi
2024-06-03 10:01:06 +02:00
# Write PID file
log ":: Writing PID"
echo "$$" > "/tmp/jobrunner/jobrunner.pid"
2023-12-08 17:33:11 +01:00
2024-06-03 10:01:06 +02:00
log ":: Started jobrunner daemon"
2023-12-08 17:33:11 +01:00
2024-06-03 10:01:06 +02:00
# Enter loop
2023-12-08 17:33:11 +01:00
while true; do
2024-06-03 10:01:06 +02:00
# 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
2024-06-03 10:29:56 +02:00
log ":: Executing command"
2024-06-03 10:01:06 +02:00
# 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
2024-06-03 10:29:56 +02:00
log ":: Exiting, as requested"
2024-06-03 10:01:06 +02:00
logcmd rm -rf "/tmp/jobrunner/"
exit 0
2023-12-08 17:33:11 +01:00
fi
2024-06-03 10:01:06 +02:00
# Execute command in subprocess
2024-06-03 10:29:56 +02:00
echo ":: Execute: ${JOBRUNNER_COMMAND}" &>> /tmp/jobrunner/jobrunner.log
( ${JOBRUNNER_COMMAND} ) &>> /tmp/jobrunner/jobrunner.log &
2024-06-03 10:01:06 +02:00
fi
# Sleep specified amount
sleep "${JOBRUNNER_TIMEOUT}"
2023-12-08 17:33:11 +01:00
done