safe_asterisk 1.8 KB

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