safe_asterisk 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/bin/sh
  2. TTY=9 # TTY (if you want one) for Asterisk to run on
  3. CONSOLE=yes # Whether or not you want a console
  4. #NOTIFY=ben@alkaloid.net # Who to notify about crashes
  5. DUMPDROP=/tmp
  6. #
  7. # Don't fork when running "safely"
  8. #
  9. ASTARGS=""
  10. if [ "$TTY" != "" ]; then
  11. if [ -c /dev/tty${TTY} ]; then
  12. TTY=tty${TTY}
  13. elif [ -c /dev/vc/${TTY} ]; then
  14. TTY=vc/${TTY}
  15. else
  16. echo "Cannot find your TTY (${TTY})" >&2
  17. exit 1
  18. fi
  19. ASTARGS="${ASTARGS} -vvvg"
  20. if [ "$CONSOLE" != "no" ]; then
  21. ASTARGS="${ASTARGS} -c"
  22. fi
  23. fi
  24. if [ ! -w ${DUMPDROP} ]; then
  25. echo "Cannot write to ${DUMPDROP}" >&2
  26. exit 1
  27. fi
  28. #
  29. # Let Asterisk dump core
  30. #
  31. ulimit -c unlimited
  32. #launch_asterisk()
  33. #{
  34. #}
  35. run_asterisk()
  36. {
  37. while :; do
  38. if [ "$TTY" != "" ]; then
  39. cd /tmp
  40. stty sane < /dev/${TTY}
  41. asterisk ${ASTARGS} >& /dev/${TTY} < /dev/${TTY}
  42. else
  43. cd /tmp
  44. asterisk ${ASTARGS}
  45. fi
  46. EXITSTATUS=$?
  47. echo "Asterisk ended with exit status $EXITSTATUS"
  48. if [ "$EXITSTATUS" = "0" ]; then
  49. # Properly shutdown....
  50. echo "Asterisk shutdown normally."
  51. exit 0
  52. elif [ $EXITSTATUS -gt 128 ]; then
  53. let EXITSIGNAL=EXITSTATUS-128
  54. echo "Asterisk exited on signal $EXITSIGNAL."
  55. if [ "$NOTIFY" != "" ]; then
  56. echo "Asterisk exited on signal $EXITSIGNAL. Might want to take a peek." | \
  57. mail -s "Asterisk Died" $NOTIFY
  58. fi
  59. if [ -f /tmp/core ]; then
  60. mv /tmp/core ${DUMPDROP}/core.`hostname`-`date -Iseconds` &
  61. fi
  62. else
  63. echo "Asterisk died with code $EXITSTATUS. Aborting."
  64. if [ -f /tmp/core ]; then
  65. mv /tmp/core ${DUMPDROP}/core.`hostname`-`date -Iseconds` &
  66. fi
  67. exit 0
  68. fi
  69. echo "Automatically restarting Asterisk."
  70. done
  71. }
  72. run_asterisk &