unimrcp-server 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #!/bin/sh
  2. #
  3. # unimrcp-server This shell script takes care of starting and stopping the UniMRCP server.
  4. #
  5. # chkconfig: 2345 65 35
  6. # description: UniMRCP is an open source MRCP v1 & v2 server.
  7. # Some global variables
  8. # Application
  9. APP_NAME="unimrcpserver"
  10. APP_LONG_NAME="UniMRCP Server"
  11. UNIMRCP_DIR="/usr/local/unimrcp/"
  12. DAEMON_ARGS="-d -r ${UNIMRCP_DIR}"
  13. APP_ARGS="-o 2"
  14. EXEC="${UNIMRCP_DIR}bin/${APP_NAME}"
  15. # sudo user
  16. USERNAME=root
  17. # Priority at which to run the server. See "man nice" for valid priorities.
  18. # nice is only used if a priority is specified.
  19. PRIORITY=
  20. # Location of the pid file.
  21. PIDDIR="/var/run/"
  22. pid=
  23. if [ -e $PIDDIR ]; then
  24. echo
  25. else
  26. mkdir $PIDDIR
  27. fi
  28. # Allow configuration overrides in /etc/sysconfig/$APP_NAME
  29. CONFIGFILE=/etc/sysconfig/$APP_NAME
  30. [ -x $CONFIGFILE ] && . $CONFIGFILE
  31. # Do not modify anything beyond this point
  32. #-----------------------------------------------------------------------------
  33. # Get the fully qualified path to the script
  34. case $0 in
  35. /*)
  36. SCRIPT="$0"
  37. ;;
  38. *)
  39. PWD=`pwd`
  40. SCRIPT="$PWD/$0"
  41. ;;
  42. esac
  43. # Change spaces to ":" so the tokens can be parsed.
  44. SCRIPT=`echo $SCRIPT | sed -e 's; ;:;g'`
  45. # Get the real path to this script, resolving any symbolic links
  46. TOKENS=`echo $SCRIPT | sed -e 's;/; ;g'`
  47. REALPATH=
  48. for C in $TOKENS; do
  49. REALPATH="$REALPATH/$C"
  50. while [ -h "$REALPATH" ] ; do
  51. LS="`ls -ld "$REALPATH"`"
  52. LINK="`expr "$LS" : '.*-> \(.*\)$'`"
  53. if expr "$LINK" : '/.*' > /dev/null; then
  54. REALPATH="$LINK"
  55. else
  56. REALPATH="`dirname "$REALPATH"`""/$LINK"
  57. fi
  58. done
  59. done
  60. # Change ":" chars back to spaces.
  61. REALPATH=`echo $REALPATH | sed -e 's;:; ;g'`
  62. # Change the current directory to the location of the script
  63. cd "`dirname "$REALPATH"`"
  64. chown $USERNAME $PIDDIR
  65. # Process ID
  66. PIDFILE="$PIDDIR/$APP_NAME.pid"
  67. # Resolve the location of the 'ps' command
  68. PSEXE="/usr/bin/ps"
  69. if [ ! -x $PSEXE ]
  70. then
  71. PSEXE="/bin/ps"
  72. if [ ! -x $PSEXE ]
  73. then
  74. echo "Unable to locate 'ps'."
  75. echo "Please report this with the location on your system."
  76. exit 1
  77. fi
  78. fi
  79. # Build the nice clause
  80. if [ "X$PRIORITY" = "X" ]
  81. then
  82. CMDNICE=""
  83. else
  84. CMDNICE="nice -$PRIORITY"
  85. fi
  86. getpid() {
  87. if [ -f $PIDFILE ]
  88. then
  89. if [ -r $PIDFILE ]
  90. then
  91. pid=`cat $PIDFILE`
  92. if [ "X$pid" != "X" ]
  93. then
  94. # Verify that a process with this pid is still running.
  95. pid=`$PSEXE -p $pid | grep $pid | grep -v grep | awk '{print $1}' | tail -1`
  96. if [ "X$pid" = "X" ]
  97. then
  98. # This is a stale pid file.
  99. rm -f $PIDFILE
  100. echo "Removed stale pid file: $PIDFILE"
  101. fi
  102. fi
  103. else
  104. echo "Cannot read $PIDFILE."
  105. exit 1
  106. fi
  107. fi
  108. }
  109. testpid() {
  110. pid=`$PSEXE -p $pid | grep $pid | grep -v grep | awk '{print $1}' | tail -1`
  111. if [ "X$pid" = "X" ]
  112. then
  113. # Process is gone so remove the pid file.
  114. rm -f $PIDFILE
  115. fi
  116. }
  117. console() {
  118. echo "Running $APP_LONG_NAME..."
  119. getpid
  120. if [ "X$pid" = "X" ]
  121. then
  122. exec sudo -u $USERNAME $CMDNICE $EXEC $APP_ARGS
  123. echo $pid > $PIDFILE
  124. else
  125. echo "$APP_LONG_NAME is already running."
  126. exit 1
  127. fi
  128. }
  129. start() {
  130. echo "Starting $APP_LONG_NAME..."
  131. getpid
  132. if [ "X$pid" = "X" ]
  133. then
  134. sudo -u $USERNAME $CMDNICE $EXEC $DAEMON_ARGS $APP_ARGS
  135. pid=`$PSEXE -C $APP_NAME -o pid=`
  136. echo $pid > $PIDFILE
  137. else
  138. echo "$APP_LONG_NAME is already running."
  139. exit 1
  140. fi
  141. }
  142. stopit() {
  143. echo "Stopping $APP_LONG_NAME..."
  144. getpid
  145. if [ "X$pid" = "X" ]
  146. then
  147. echo "$APP_LONG_NAME was not running."
  148. else
  149. # Running so try to stop it.
  150. sudo -u $USERNAME kill $pid
  151. if [ $? -ne 0 ]
  152. then
  153. # An explanation for the failure should have been given
  154. echo "Unable to stop $APP_LONG_NAME."
  155. exit 1
  156. fi
  157. # We can not predict how long it will take for the wrapper to
  158. # actually stop as it depends on settings in wrapper.conf.
  159. # Loop until it does.
  160. savepid=$pid
  161. CNT=0
  162. TOTCNT=0
  163. while [ "X$pid" != "X" ]
  164. do
  165. # Loop for up to 5 minutes
  166. if [ "$TOTCNT" -lt "300" ]
  167. then
  168. if [ "$CNT" -lt "5" ]
  169. then
  170. CNT=`expr $CNT + 1`
  171. else
  172. echo "Waiting for $APP_LONG_NAME to exit..."
  173. CNT=0
  174. fi
  175. TOTCNT=`expr $TOTCNT + 1`
  176. sleep 1
  177. testpid
  178. else
  179. pid=
  180. fi
  181. done
  182. pid=$savepid
  183. testpid
  184. if [ "X$pid" != "X" ]
  185. then
  186. echo "Timed out waiting for $APP_LONG_NAME to exit."
  187. echo " Attempting a forced exit..."
  188. kill -9 $pid
  189. fi
  190. pid=$savepid
  191. testpid
  192. if [ "X$pid" != "X" ]
  193. then
  194. echo "Failed to stop $APP_LONG_NAME."
  195. exit 1
  196. else
  197. echo "Stopped $APP_LONG_NAME."
  198. fi
  199. fi
  200. }
  201. status() {
  202. getpid
  203. if [ "X$pid" = "X" ]
  204. then
  205. echo "$APP_LONG_NAME is not running."
  206. exit 1
  207. else
  208. echo "$APP_LONG_NAME is running ($pid)."
  209. exit 0
  210. fi
  211. }
  212. case "$1" in
  213. 'console')
  214. console
  215. ;;
  216. 'start')
  217. start
  218. ;;
  219. 'stop')
  220. stopit
  221. ;;
  222. 'restart')
  223. stopit
  224. start
  225. ;;
  226. 'status')
  227. status
  228. ;;
  229. *)
  230. echo "Usage: $0 { console | start | stop | restart | status }"
  231. exit 1
  232. ;;
  233. esac
  234. exit 0