I use the following to monitor my PJSip trunk and attempt to restart asterisk if my trunk becomes unregistered. If it fails 6 times it reboots the server. The logic could be adjusted if you don't want a reboot, but it's the basic principal of a never ending script that was hard for me to pin down.
Note: I call this via cron like so
cat << 'EOF' >/etc/cron.d/gvsipcheck.atreboot SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root HOME=/ @reboot root /srv/scripts/gvsipcheck.sh EOF
Script:
#!/bin/bash pwd="/srv/scripts" count=0 echo "Script Started on $(date) 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) Failure.Count=$count" >> "$pwd/failures.count" fi if [ "$count" -gt 5 ]; then echo "Server Reboot due to Failure.Count=$count on $(date)" >> "$pwd/reboot.notification" reboot fi done