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
@reboot root /srv/scripts/gvsipcheck.sh
echo "Script Started on $(date) Failure.Count=$count" >> "$pwd/failures.count"
while [ $start = "start" ]; do
var="$(asterisk -rx "pjsip show registrations" | grep -o Registered)"
if [ "$var" != "Registered" ]; then
count=$(( $count + 1 ))
echo "Trunk Failure on $(date) Failure.Count=$count" >> "$pwd/failures.count"
if [ "$count" -gt 5 ]; then
echo "Server Reboot due to Failure.Count=$count on $(date)" >> "$pwd/reboot.notification"