safe_asterisk 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #!/bin/sh
  2. ASTETCDIR="__ASTERISK_ETC_DIR__"
  3. ASTSBINDIR="__ASTERISK_SBIN_DIR__"
  4. ASTVARRUNDIR="__ASTERISK_VARRUN_DIR__"
  5. ASTVARLOGDIR="__ASTERISK_LOG_DIR__"
  6. CLIARGS="$*" # Grab any args passed to safe_asterisk
  7. TTY=9 # TTY (if you want one) for Asterisk to run on
  8. CONSOLE=yes # Whether or not you want a console
  9. #NOTIFY=root@localhost # Who to notify about crashes
  10. #EXEC=/path/to/somescript # Run this command if Asterisk crashes
  11. #LOGFILE="${ASTVARLOGDIR}/safe_asterisk.log" # Where to place the normal logfile (disabled if blank)
  12. #SYSLOG=local0 # Which syslog facility to use (disabled if blank)
  13. MACHINE=`hostname` # To specify which machine has crashed when getting the mail
  14. DUMPDROP="${DUMPDROP:-/tmp}"
  15. RUNDIR="${RUNDIR:-/tmp}"
  16. SLEEPSECS=4
  17. ASTPIDFILE="${ASTVARRUNDIR}/asterisk.pid"
  18. # comment this line out to have this script _not_ kill all mpg123 processes when
  19. # asterisk exits
  20. KILLALLMPG123=1
  21. # run asterisk with this priority
  22. PRIORITY=0
  23. # set system filemax on supported OSes if this variable is set
  24. # SYSMAXFILES=262144
  25. # Asterisk allows full permissions by default, so set a umask, if you want
  26. # restricted permissions.
  27. #UMASK=022
  28. # set max files open with ulimit. On linux systems, this will be automatically
  29. # set to the system's maximum files open devided by two, if not set here.
  30. # MAXFILES=32768
  31. message() {
  32. if test -n "$TTY" && test "$TTY" != "no"; then
  33. echo "$1" >/dev/${TTY}
  34. fi
  35. if test -n "$SYSLOG"; then
  36. logger -p "${SYSLOG}.warn" -t safe_asterisk[$$] "$1"
  37. fi
  38. if test -n "$LOGFILE"; then
  39. echo "safe_asterisk[$$]: $1" >>"$LOGFILE"
  40. fi
  41. }
  42. # Check if Asterisk is already running. If it is, then bug out, because
  43. # starting safe_asterisk when Asterisk is running is very bad.
  44. VERSION=`"${ASTSBINDIR}/asterisk" -nrx 'core show version' 2>/dev/null`
  45. if test "`echo $VERSION | cut -c 1-8`" = "Asterisk"; then
  46. message "Asterisk is already running. $0 will exit now."
  47. exit 1
  48. fi
  49. # since we're going to change priority and open files limits, we need to be
  50. # root. if running asterisk as other users, pass that to asterisk on the command
  51. # line.
  52. # if we're not root, fall back to standard everything.
  53. if test `id -u` != 0; then
  54. echo "Oops. I'm not root. Falling back to standard prio and file max." >&2
  55. echo "This is NOT suitable for large systems." >&2
  56. PRIORITY=0
  57. message "safe_asterisk was started by `id -n` (uid `id -u`)."
  58. else
  59. if `uname -s | grep Linux >/dev/null 2>&1`; then
  60. # maximum number of open files is set to the system maximum
  61. # divided by two if MAXFILES is not set.
  62. if test -z "$MAXFILES"; then
  63. # just check if file-max is readable
  64. if test -r /proc/sys/fs/file-max; then
  65. MAXFILES=$((`cat /proc/sys/fs/file-max` / 2))
  66. # don't exceed upper limit of 2^20 for open
  67. # files on systems where file-max is > 2^21
  68. if test $MAXFILES -gt 1048576; then
  69. MAXFILES=1048576
  70. fi
  71. fi
  72. fi
  73. SYSCTL_MAXFILES="fs.file-max"
  74. elif `uname -s | grep Darwin /dev/null 2>&1`; then
  75. SYSCTL_MAXFILES="kern.maxfiles"
  76. fi
  77. if test -n "$SYSMAXFILES"; then
  78. if test -n "$SYSCTL_MAXFILES"; then
  79. sysctl -w $SYSCTL_MAXFILES=$SYSMAXFILES
  80. fi
  81. fi
  82. # set the process's filemax to whatever set above
  83. ulimit -n $MAXFILES
  84. if test ! -d "${ASTVARRUNDIR}"; then
  85. mkdir -p "${ASTVARRUNDIR}"
  86. chmod 770 "${ASTVARRUNDIR}"
  87. fi
  88. fi
  89. if test -n "$UMASK"; then
  90. umask $UMASK
  91. fi
  92. #
  93. # Let Asterisk dump core
  94. #
  95. ulimit -c unlimited
  96. #
  97. # Don't fork when running "safely"
  98. #
  99. ASTARGS=""
  100. if test -n "$TTY" && test "$TTY" != "no"; then
  101. if test -c /dev/tty${TTY}; then
  102. TTY=tty${TTY}
  103. elif test -c /dev/vc/${TTY}; then
  104. TTY=vc/${TTY}
  105. elif test "$TTY" = "9"; then # ignore default if it was untouched
  106. # If there is no /dev/tty9 and not /dev/vc/9 we don't
  107. # necessarily want to die at this point. Pretend that
  108. # TTY wasn't set.
  109. TTY=
  110. else
  111. message "Cannot find specified TTY (${TTY})"
  112. exit 1
  113. fi
  114. if test -n "$TTY"; then
  115. ASTARGS="${ASTARGS} -vvvg"
  116. if test "$CONSOLE" != "no"; then
  117. ASTARGS="${ASTARGS} -c"
  118. fi
  119. fi
  120. fi
  121. if test ! -d "${RUNDIR}"; then
  122. message "${RUNDIR} does not exist, creating"
  123. if ! mkdir -p "${RUNDIR}"; then
  124. message "Unable to create ${RUNDIR}"
  125. exit 1
  126. fi
  127. fi
  128. if test ! -w "${DUMPDROP}"; then
  129. message "Cannot write to ${DUMPDROP}"
  130. exit 1
  131. fi
  132. #
  133. # Don't die if stdout/stderr can't be written to
  134. #
  135. trap '' PIPE
  136. #
  137. # Run scripts to set any environment variables or do any other system-specific setup needed
  138. #
  139. if test -d "${ASTETCDIR}/startup.d"; then
  140. for script in "${ASTETCDIR}/startup.d/"*.sh; do
  141. if test -r "${script}"; then
  142. . "${script}"
  143. fi
  144. done
  145. fi
  146. run_asterisk()
  147. {
  148. while :; do
  149. if test -n "$TTY" && test "$TTY" != "no"; then
  150. cd "${RUNDIR}"
  151. stty sane </dev/${TTY}
  152. nice -n $PRIORITY "${ASTSBINDIR}/asterisk" -f ${CLIARGS} ${ASTARGS} >/dev/${TTY} 2>&1 </dev/${TTY}
  153. else
  154. cd "${RUNDIR}"
  155. nice -n $PRIORITY "${ASTSBINDIR}/asterisk" -f ${CLIARGS} ${ASTARGS} >/dev/null 2>&1 </dev/null
  156. fi
  157. EXITSTATUS=$?
  158. message "Asterisk ended with exit status $EXITSTATUS"
  159. if test $EXITSTATUS -eq 0; then
  160. # Properly shutdown....
  161. message "Asterisk shutdown normally."
  162. exit 0
  163. elif test $EXITSTATUS -gt 128; then
  164. EXITSIGNAL=$((EXITSTATUS - 128))
  165. message "Asterisk exited on signal $EXITSIGNAL."
  166. if test -n "$NOTIFY"; then
  167. echo "Asterisk on $MACHINE exited on signal $EXITSIGNAL. Might want to take a peek." | \
  168. mail -s "Asterisk on $MACHINE died (sig $EXITSIGNAL)" $NOTIFY
  169. fi
  170. if test -n "$EXEC"; then
  171. $EXEC
  172. fi
  173. PID=`cat ${ASTPIDFILE}`
  174. DATE=`date "+%Y-%m-%dT%H:%M:%S%z"`
  175. if test -f "${RUNDIR}/core.${PID}"; then
  176. mv "${RUNDIR}/core.${PID}" "${DUMPDROP}/core.`hostname`-$DATE" &
  177. elif test -f "${RUNDIR}/core"; then
  178. mv "${RUNDIR}/core" "${DUMPDROP}/core.`hostname`-$DATE" &
  179. fi
  180. else
  181. message "Asterisk died with code $EXITSTATUS."
  182. PID=`cat ${ASTPIDFILE}`
  183. DATE=`date "+%Y-%m-%dT%H:%M:%S%z"`
  184. if test -f "${RUNDIR}/core.${PID}"; then
  185. mv "${RUNDIR}/core.${PID}" "${DUMPDROP}/core.`hostname`-$DATE" &
  186. elif test -f "${RUNDIR}/core"; then
  187. mv "${RUNDIR}/core" "${DUMPDROP}/core.`hostname`-$DATE" &
  188. fi
  189. fi
  190. message "Automatically restarting Asterisk."
  191. sleep $SLEEPSECS
  192. if test "0$KILLALLMPG123" -gt 0; then
  193. pkill -9 mpg123
  194. fi
  195. done
  196. }
  197. if test -n "$ASTSAFE_FOREGROUND"; then
  198. run_asterisk
  199. else
  200. run_asterisk &
  201. fi