rc.mandrake.asterisk 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #!/bin/sh
  2. #
  3. # asterisk: Starts the asterisk service
  4. #
  5. # Version: @(#) /etc/rc.d/init.d/asterisk 1.0
  6. #
  7. # chkconfig: 2345 95 10
  8. # description: Starts the asterisk service
  9. #
  10. # processname: asterisk
  11. #
  12. TTY=9 # TTY (if you want one) for Asterisk to run on
  13. CONSOLE=yes # Whether or not you want a console
  14. NOTIFY=root # Who to notify about crashes
  15. DUMPDROP=/tmp
  16. HOSTNAME=`hostname`
  17. # Setup environment
  18. cd /usr/src
  19. OPENH323DIR=/usr/src/openh323
  20. PWLIBDIR=/usr/src/pwlib
  21. LD_LIBRARY_PATH=$OPENH323DIR/lib:$PWLIBDIR/lib
  22. export OPENH323DIR PWLIBDIR LD_LIBRARY_PATH
  23. # Source function library.
  24. . /etc/rc.d/init.d/functions
  25. #
  26. # Don't fork when running "safely"
  27. #
  28. ASTARGS="-p"
  29. if [ "$TTY" != "" ]; then
  30. if [ -c /dev/tty${TTY} ]; then
  31. TTY=tty${TTY}
  32. elif [ -c /dev/vc/${TTY} ]; then
  33. TTY=vc/${TTY}
  34. else
  35. echo "Cannot find your TTY (${TTY})" >&2
  36. exit 1
  37. fi
  38. ASTARGS="${ASTARGS} -vvv"
  39. if [ "$CONSOLE" != "no" ]; then
  40. ASTARGS="${ASTARGS} -c"
  41. fi
  42. fi
  43. if [ ! -w ${DUMPDROP} ]; then
  44. echo "Cannot write to ${DUMPDROP}" >&2
  45. exit 1
  46. fi
  47. #
  48. # Let Asterisk dump core
  49. #
  50. ulimit -c unlimited
  51. #launch_asterisk()
  52. #{
  53. #}
  54. run_asterisk()
  55. {
  56. while :; do
  57. if [ "$TTY" != "" ]; then
  58. cd /tmp
  59. stty sane < /dev/${TTY}
  60. asterisk ${ASTARGS} > /dev/${TTY} 2>&1 < /dev/${TTY}
  61. else
  62. cd /tmp
  63. asterisk ${ASTARGS}
  64. fi
  65. EXITSTATUS=$?
  66. echo "Asterisk ended with exit status $EXITSTATUS"
  67. if [ "$EXITSTATUS" = "0" ]; then
  68. # Properly shutdown....
  69. echo "Asterisk shutdown normally."
  70. exit 0
  71. elif [ $EXITSTATUS -gt 128 ]; then
  72. if [ "$EXITSTATUS" = "129" ]; then
  73. EXITSIGNAL=1
  74. EXITMSG="Hangup"
  75. elif [ "$EXITSTATUS" = "130" ]; then
  76. EXITSIGNAL=2
  77. EXITMSG="Interrupt"
  78. elif [ "$EXITSTATUS" = "131" ]; then
  79. EXITSIGNAL=3
  80. EXITMSG="Quit"
  81. elif [ "$EXITSTATUS" = "132" ]; then
  82. EXITSIGNAL=4
  83. EXITMSG="Illegal instruction"
  84. elif [ "$EXITSTATUS" = "133" ]; then
  85. EXITSIGNAL=5
  86. EXITMSG="Trace trap"
  87. elif [ "$EXITSTATUS" = "134" ]; then
  88. EXITSIGNAL=6
  89. EXITMSG="IOT Trap"
  90. elif [ "$EXITSTATUS" = "135" ]; then
  91. EXITSIGNAL=7
  92. EXITMSG="Bus Error"
  93. elif [ "$EXITSTATUS" = "136" ]; then
  94. EXITSIGNAL=8
  95. EXITMSG="Floating-point exception"
  96. elif [ "$EXITSTATUS" = "137" ]; then
  97. EXITSIGNAL=9
  98. EXITMSG="Killed"
  99. elif [ "$EXITSTATUS" = "138" ]; then
  100. EXITSIGNAL=10
  101. EXITMSG="User-defined signal 1"
  102. elif [ "$EXITSTATUS" = "139" ]; then
  103. EXITSIGNAL=11
  104. EXITMSG="Segmentation violation"
  105. elif [ "$EXITSTATUS" = "140" ]; then
  106. EXITSIGNAL=12
  107. EXITMSG="User-defined signal 2"
  108. elif [ "$EXITSTATUS" = "141" ]; then
  109. EXITSIGNAL=13
  110. EXITMSG="Broken pipe"
  111. elif [ "$EXITSTATUS" = "142" ]; then
  112. EXITSIGNAL=14
  113. EXITMSG="Alarm clock"
  114. elif [ "$EXITSTATUS" = "143" ]; then
  115. EXITSIGNAL=15
  116. EXITMSG="Termination"
  117. elif [ "$EXITSTATUS" = "144" ]; then
  118. EXITSIGNAL=16
  119. EXITMSG="Stack fault"
  120. fi
  121. echo "Asterisk exited on signal $EXITSIGNAL - $EXITMSG."
  122. if [ "$NOTIFY" != "" ]; then
  123. echo "Asterisk exited on signal $EXITSIGNAL - $EXITMSG. Might want to take a peek." | \
  124. mail -s "Asterisk Died ($HOSTNAME)" $NOTIFY
  125. fi
  126. if [ -f /tmp/core ]; then
  127. mv /tmp/core ${DUMPDROP}/core.`hostname`-`date -Iseconds` &
  128. fi
  129. else
  130. echo "Asterisk died with code $EXITSTATUS. Aborting."
  131. if [ -f /tmp/core ]; then
  132. mv /tmp/core ${DUMPDROP}/core.`hostname`-`date -Iseconds` &
  133. fi
  134. exit 0
  135. fi
  136. echo "Automatically restarting Asterisk."
  137. done
  138. }
  139. case "$1" in
  140. start)
  141. gprintf "Starting asterisk: "
  142. run_asterisk >/dev/null 2>&1 &
  143. sleep 2 # Give it time to die
  144. succeeded=`pidof asterisk|awk '{print NF}'`
  145. if [ $succeeded = "0" ]; then
  146. failure
  147. else
  148. success
  149. fi
  150. echo
  151. ;;
  152. stop)
  153. gprintf "Stopping asterisk: "
  154. asterisk -r -x "stop gracefully" >/dev/null 2>&1
  155. killall -9 mpg123 2>/dev/null
  156. success
  157. echo
  158. ;;
  159. restart)
  160. $0 stop
  161. usleep 100000
  162. $0 start
  163. ;;
  164. reload)
  165. gprintf "Reloading asterisk: "
  166. asterisk -r -x "reload" >/dev/null 2>&1
  167. success
  168. echo
  169. ;;
  170. stopnow)
  171. gprintf "Stopping asterisk: "
  172. asterisk -r -x "stop now" >/dev/null 2>&1
  173. success
  174. echo
  175. ;;
  176. restartnow)
  177. $0 stopnow
  178. $0 start
  179. ;;
  180. status)
  181. succeeded=`pidof asterisk|awk '{print NF}'`
  182. if [ $succeeded = "0" ]; then
  183. echo "Asterisk is not running"
  184. else
  185. echo "Asterisk is currently running with $succeeded threads"
  186. fi
  187. ;;
  188. *)
  189. gprintf "*** Usage: $0 {start|stop|reload|restart|stopnow|restartnow|status}\n"
  190. exit 1
  191. esac
  192. exit 0