rc.mandriva.asterisk 4.7 KB

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