Created
March 22, 2023 21:34
-
-
Save Crypt0s/9e37c7d0b2623b9d5cf77ff3feb5b3b0 to your computer and use it in GitHub Desktop.
Bash script to react to a program that restarts X times in Y seconds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Max number of restarts in THRESH seconds | |
MAX=5 | |
THRESH=10 | |
# Array to hold the list of restart dates | |
THEN=() | |
# run this monitoring operation forever | |
while : | |
do | |
# The blocking thing you want to monitor should go here. | |
read PROMPT | |
# if we got here, the blocking operation returned so whatever we were monitoring needs to be restarted | |
# record the time that it exited | |
NOW=`date +%s` | |
THEN+=($NOW) | |
# instance the counter | |
Z=0 | |
# look at each restart in the list of restarts | |
for i in ${!THEN[@]} | |
do | |
# review the list of restart dates for dates over or under the THRESH | |
if [[ ${THEN[$i]} -gt $(($NOW-$THRESH)) ]] | |
then | |
# count the number of restarts inside of THRESH | |
Z=$((Z+1)) | |
else | |
# groom the list of restart dates | |
unset 'THEN[i]' | |
fi | |
if [[ $Z -gt MAX ]] | |
then | |
# Your reaction to the monitored operation restarting over MAX times in THRESH seconds goes here | |
echo "!!!!!!!!!!!!!!!" | |
fi | |
done | |
done | |
# if you got here, something went real wrong ;) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment