safe_asterisk 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. MACHINE=`hostname` # To specify which machine has crashed when getting the mail
  7. DUMPDROP=/tmp
  8. ASTSBINDIR=__ASTERISK_SBIN_DIR__
  9. ASTPIDFILE=__ASTERISK_VARRUN_DIR__/asterisk.pid
  10. #
  11. # Don't fork when running "safely"
  12. #
  13. ASTARGS=""
  14. if [ "$TTY" != "" ]; then
  15. if [ -c /dev/tty${TTY} ]; then
  16. TTY=tty${TTY}
  17. elif [ -c /dev/vc/${TTY} ]; then
  18. TTY=vc/${TTY}
  19. else
  20. echo "Cannot find your TTY (${TTY})" >&2
  21. exit 1
  22. fi
  23. ASTARGS="${ASTARGS} -vvvg"
  24. if [ "$CONSOLE" != "no" ]; then
  25. ASTARGS="${ASTARGS} -c"
  26. fi
  27. fi
  28. if [ ! -w ${DUMPDROP} ]; then
  29. echo "Cannot write to ${DUMPDROP}" >&2
  30. exit 1
  31. fi
  32. #
  33. # Let Asterisk dump core
  34. #
  35. ulimit -c unlimited
  36. #
  37. # Don't die if stdout/stderr can't be written to
  38. #
  39. trap '' PIPE
  40. #
  41. # Run scripts to set any environment variables or do any other system-specific setup needed
  42. #
  43. if [ -d /etc/asterisk/startup.d ]; then
  44. for script in /etc/asterisk/startup.d/*.sh; do
  45. if [ -x ${script} ]; then
  46. source ${script}
  47. fi
  48. done
  49. fi
  50. run_asterisk()
  51. {
  52. while :; do
  53. if [ "$TTY" != "" ]; then
  54. cd /tmp
  55. stty sane < /dev/${TTY}
  56. ${ASTSBINDIR}/asterisk -f ${CLIARGS} ${ASTARGS} >& /dev/${TTY} < /dev/${TTY}
  57. else
  58. cd /tmp
  59. ${ASTSBINDIR}/asterisk -f ${CLIARGS} ${ASTARGS}
  60. fi
  61. EXITSTATUS=$?
  62. echo "Asterisk ended with exit status $EXITSTATUS"
  63. if [ "$EXITSTATUS" = "0" ]; then
  64. # Properly shutdown....
  65. echo "Asterisk shutdown normally."
  66. exit 0
  67. elif [ $EXITSTATUS -gt 128 ]; then
  68. let EXITSIGNAL=EXITSTATUS-128
  69. echo "Asterisk exited on signal $EXITSIGNAL."
  70. if [ "$NOTIFY" != "" ]; then
  71. echo "Asterisk on $MACHINE exited on signal $EXITSIGNAL. Might want to take a peek." | \
  72. mail -s "Asterisk Died" $NOTIFY
  73. fi
  74. PID=`cat ${ASTPIDFILE}`
  75. if [ -f /tmp/core.${PID} ]; then
  76. mv /tmp/core.${PID} ${DUMPDROP}/core.`hostname`-`date -Iseconds` &
  77. elif [ -f /tmp/core ]; then
  78. mv /tmp/core ${DUMPDROP}/core.`hostname`-`date -Iseconds` &
  79. fi
  80. else
  81. if [ "${EXITSTATUS}" = "0" ]; then
  82. echo "Asterisk ended normally. Aborting."
  83. exit 0
  84. else
  85. echo "Asterisk died with code $EXITSTATUS."
  86. PID=`cat ${ASTPIDFILE}`
  87. if [ -f /tmp/core.${PID} ]; then
  88. mv /tmp/core.${PID} ${DUMPDROP}/core.`hostname`-`date -Iseconds` &
  89. elif [ -f /tmp/core ]; then
  90. mv /tmp/core ${DUMPDROP}/core.`hostname`-`date -Iseconds` &
  91. fi
  92. fi
  93. fi
  94. echo "Automatically restarting Asterisk."
  95. sleep 4
  96. done
  97. }
  98. run_asterisk &