#!/bin/bash
#
trouble () {
trap - EXIT
kill $id
date
~/asterisk/asterisk-1.6.2.9/main/asterisk -rx 'core show uptime'
~/asterisk/asterisk-1.6.2.9/main/asterisk -rx 'core show locks'
printf 'bt\nbt full\nthread apply all bt\n' | gdb asterisk $(cat /var/run/asterisk/asterisk.pid)
killall -9 asterisk
exit
}

trap 'echo trouble ; trouble >/tmp/$(date "+asterisk-restart-%Y-%m-%d-%H%M").txt' EXIT
(trap - EXIT; sleep 10; kill $$)&
id=$!
if /usr/local/bin/sipsak -s sip:nobody@sip.this.server
then
    kill $id
    trap - EXIT
    exit
fi
#
trap - EXIT
trouble >/tmp/$(date "+asterisk-restart-%Y-%m-%d-%H%M").txt
exit