0

I'm writing a script to monitor my sip trunk and attempt to fix it. If it fails to fix the issue 6 times, then reboot the server. The script is called by cron via @reboot. I first had nested While Loops but that didn't work correctly so I switched to a never ending While Loop with two nested If Loops to perform the functions of the script.

I was wondering if somebody could take a quick look and see if the way I am attacking it makes sense and is logical approach.

Thank You,

Script as it stands:

#!/bin/bash

pwd="/srv/scripts"
count=0
echo "Script Started on $(date -u) Failure.Count=$count" >> "$pwd/failures.count"

start=start
while [ $start = "start" ]; do

sleep 420

var="$(asterisk -rx "pjsip show registrations" | grep -o Registered)"

    if [ "$var" != "Registered" ]; then
        amportal restart
        count=$(( $count + 1 ))
        echo "Trunk Failure on $(date -u) Failure.Count=$count" >> "$pwd/failures.count"
    fi

    if [ "$count" -gt 5 ]; then  
        echo "Server Reboot due to Failure.Count=$count on $(date -u)" >> "$pwd/reboot.notification"  
        reboot    
    fi
done
6
  • it seems ok. I just couldnt understand the use of var=init Commented Dec 10, 2018 at 9:25
  • I'm not sure if this is some sort of violation for this SE, as there is nothing wrong with it at the moment, but I wanted feedback on the logic, I've never done nested loops really. I just used var=init to set the variable to anything but "Registered" off the hop, perhaps I could have not, but I'm used to setting variables early on. This is kinda a "freebie" if somebody feels confident enough in their programming knowledge to answer The Logic is Sound Commented Dec 10, 2018 at 9:28
  • @Simonare You should have seen the nested While Loops version, what a nightmare to read vs this, as soon as I thought, how about nested If/Then it sorta clicked that was the correct way. Commented Dec 10, 2018 at 9:30
  • your solution does not include nested if. but you can use it of course Commented Dec 10, 2018 at 9:33
  • @Simonare The if/then is nested in the While Loop is what I meant. Commented Dec 10, 2018 at 9:35

1 Answer 1

1

There is no need to use a variable in the while loop, or to capture the grep output into a variable.

#!/bin/bash

pwd="/srv/scripts"
count=0
echo "Script Started on $(date -u) Failure.Count=$count" >> "$pwd/failures.count"

# No need for a variable here
while true; do
    # Fix indentation
    sleep 420

    # Again, no need for a variable; use grep -q
    if ! asterisk -rx "pjsip show registrations" | grep -q Registered
    then
        amportal restart
        count=$(( $count + 1 ))
        echo "Trunk Failure on $(date -u) Failure.Count=$count" >> "$pwd/failures.count"
    fi

    if [ "$count" -gt 5 ]; then  
        echo "Server Reboot due to Failure.Count=$count on $(date -u)" >> "$pwd/reboot.notification"  
        reboot    
    fi
done

I would perhaps also collect all the log notices in a single log file, and use a more traditional log format with a time stamp and the script's name bofore each message.

Should the counter reset to zero if you see a success? Having the server reboot because you disconnected the network cable at the wrong time seems like something you'd want to avoid.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.