This version collects the events in a separate thread, and doesn't gobble up events. So hopefully it's more robust, in addition to being easier to read. One downside of using the "watchdog" module is that it only exposes file/dir modification events. So if I ever want to keep track of opened files, I'll have to call inotifywait again.
58 lines
1.2 KiB
Bash
Executable File
58 lines
1.2 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
function ts {
|
|
echo [`date '+%Y-%m-%d %H:%M:%S'`] MASTER:
|
|
}
|
|
|
|
echo "$(ts) Starting master controller"
|
|
|
|
if [ -f /config/sample.conf ]; then
|
|
echo "$(ts) /config/sample.conf exists. Rename it, check the settings, then rerun the container. Exiting."
|
|
exit 1
|
|
fi
|
|
|
|
readarray -t CONFIG_FILES < <(ls /config/*.conf)
|
|
|
|
# If there is no config file copy the default one
|
|
if [[ "$CONFIG_FILES" == "" ]]
|
|
then
|
|
echo "$(ts) Creating sample config file. Rename it, check the settings, then rerun the container. Exiting."
|
|
cp /files/sample.conf /config/sample.conf
|
|
chmod a+w /config/sample.conf
|
|
exit 1
|
|
fi
|
|
|
|
PIDS=()
|
|
|
|
for CONFIG_FILE in "${CONFIG_FILES[@]}"
|
|
do
|
|
echo "$(ts) Launching monitor for $CONFIG_FILE"
|
|
/files/monitor.py $CONFIG_FILE &
|
|
PIDS+=($!)
|
|
done
|
|
|
|
# Sleep for a second to allow the monitors to check their config files
|
|
sleep 1
|
|
|
|
while true
|
|
do
|
|
for ((i = 0; i < ${#PIDS[@]}; i++))
|
|
do
|
|
if ps -p ${PIDS[$i]} > /dev/null
|
|
then
|
|
continue
|
|
fi
|
|
|
|
echo "$(ts) Monitor for ${CONFIG_FILES[$i]} has died (PID ${PIDS[$i]}). Killing other monitors and exiting."
|
|
|
|
for PID in "${PIDS[@]}"
|
|
do
|
|
kill -9 $PID >/dev/null 2>&1
|
|
done
|
|
|
|
exit 2
|
|
done
|
|
|
|
sleep 60
|
|
done
|