Skip to content

Instantly share code, notes, and snippets.

@Crypt0s
Created March 22, 2023 21:34
Show Gist options
  • Save Crypt0s/9e37c7d0b2623b9d5cf77ff3feb5b3b0 to your computer and use it in GitHub Desktop.
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
#!/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