Run Bash Script thrice & then interval for 10 mins.

Hi...

I am very new to shell scripting. I have written a script with help of this forum and some googling and it works the way I want it to. Currently this script checks for my SIP trunk registration every 5 seconds, if registration is not available then it reboots my router through telnet session and keeps rebooting it till the registration is available.

I would like the router rebooted only thrice at max and then wait for 10 minute, check for registration again and if failed then max 3 reboots and interval again.

However if the trunk registration is successful then script should keep checking it every 5 secs as per current practice.

Would be thankful for help provided.

#!/bin/bash
typeset -i interval=5
run=true
trunk=vonage
while [[ "$run" == "true" ]]; do
checktrunk=$(asterisk -rx "sip show peer $trunk" | grep -c "Status.*OK")
if [[ $checktrunk -eq 0 ]]; then
echo "$(date): SIP trunk registration failed."
sleep 2
{
sleep 2
echo admin
sleep 2
echo password
sleep 2
echo sys reboot
echo exit
} | telnet 192.168.1.1
else
echo "$(date): SIP trunk registration OK."
fi
sleep 5
done
exit 1

Try this:

#!/bin/bash
typeset -i interval=5
run=true
trunk=vonage
reboot=0
while [[ "$run" == "true" ]]; do
   checktrunk=$(asterisk -rx "sip show peer $trunk" | grep -c "Status.*OK")
   if [[ $checktrunk -eq 0 ]]; then
      echo "$(date): SIP trunk registration failed."
      sleep 2
      {
      sleep 2
      echo admin
      sleep 2
      echo password
      sleep 2
      echo sys reboot
      echo exit
      } | telnet 192.168.1.1
      (( reboot += 1 ))
      if (( reboot == 3 )); then
      	sleep 600
      	reboot=0
      fi
    else
       echo "$(date): SIP trunk registration OK."
    fi
    sleep 5 
done
exit 1
1 Like

Hi Klashxx,

This is perfect! Just what I wanted, however there's just one small problem.

During the sleep period of 600 secs (i.e ten mins break) the script does not look for registration failed or success.

For logging purposes I would like the script to check if during sleep period the trunk came back online.

If am not askin for much then please help :slight_smile: Otherwise this is perfect and solved for me! Thanks a lot.

So is what you are now asking for is a frequent poll and reboots at 10 minute intervals?

You have all you need really, you just have to adjust your logic and re-use what's been offered. Consider 600 seconds being a 120 x 5 second tests and code a loop accordingly. You'd be better trying and failing, then asking for help than asking for a ready made solution. You will then learn how things work and be better placed to support and adjust them in future.

Regards,
Robin

1 Like

Totally agree to that. Am already trying different things on it. :slight_smile: Thanks :slight_smile: :slight_smile: