asterisk.rc 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/bin/bash
  2. NAME=asterisk
  3. USER=asterisk
  4. GROUP=asterisk
  5. RUNDIR=/var/run/$NAME
  6. PIDFILE=$RUNDIR/$NAME.pid
  7. STARTCMD="(cd /; /usr/sbin/asterisk -G $GROUP -U $USER)"
  8. STOPCMD="/usr/sbin/asterisk -r -x 'core stop now'"
  9. STOPGRACECMD="/usr/sbin/asterisk -r -x 'core stop gracefully'"
  10. STOPTIMEOUT=300
  11. function getpid() {
  12. if [ -z "$PIDFILE" ]; then
  13. pid="$(pgrep -xfn "$STARTCMD")"
  14. else
  15. if [ -f "$PIDFILE" ]; then
  16. pid=$(< $PIDFILE)
  17. if [ ! -d /proc/"$pid" ]; then
  18. echo "$NAME: removing stale pidfile $PIDFILE" >&2
  19. rm -f "$PIDFILE"
  20. unset pid
  21. fi
  22. fi
  23. fi
  24. echo "$pid"
  25. }
  26. case $1 in
  27. start)
  28. pid=$(getpid)
  29. install -d -m 755 -o $USER $RUNDIR || exit 1
  30. if [ -n "$pid" ]; then
  31. echo "$NAME already running with pid $pid" >&2
  32. exit 1
  33. fi
  34. eval "$STARTCMD"
  35. ;;
  36. stop|stopnice)
  37. pid=$(getpid)
  38. if [ -n "$pid" ]; then
  39. if [ "$1" == "stop" ]; then
  40. if [ -n "$STOPCMD" ]; then
  41. eval "$STOPCMD"
  42. else
  43. kill "$pid"
  44. fi
  45. else
  46. if [ -n "$STOPCMD" ]; then
  47. eval "$STOPGRACECMD"
  48. else
  49. echo "$NAME: $1 not implemented"
  50. exit 1
  51. fi
  52. fi
  53. t=$(printf '%(%s)T' -1)
  54. tend=$((t+STOPTIMEOUT))
  55. while [ -d /proc/$pid -a $t -lt $tend ]; do
  56. sleep 0.5
  57. t=$(printf '%(%s)T' -1)
  58. done
  59. if [ -d /proc/"$pid" ]; then
  60. echo "$NAME still running with pid $pid" >&2
  61. else
  62. [ -n "$PIDFILE" ] && rm -f "$PIDFILE"
  63. fi
  64. else
  65. echo "$NAME is not running" >&2
  66. fi
  67. ;;
  68. restart)
  69. $0 stop && \
  70. $0 start
  71. ;;
  72. restartnice)
  73. $0 stopnice && \
  74. $0 start
  75. ;;
  76. status)
  77. pid=$(getpid)
  78. if [ -n "$pid" ]; then
  79. echo "$NAME is running with pid $pid"
  80. else
  81. echo "$NAME is not running"
  82. fi
  83. ;;
  84. *)
  85. echo "usage: $0 [start|stop|stopnice|restart|restartnice|status]"
  86. ;;
  87. esac