safe_asterisk_restart 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/bin/bash
  2. # vim:textwidth=80:tabstop=4:shiftwidth=4:smartindent
  3. #
  4. # this scripts prompts the user thrice, then tells asterisk to please shut down,
  5. # then kills asterisk and related processes with SIGTERM, then kills asterisk
  6. # and related processes with SIGKILL, and then starts asterisk with
  7. # safe_asterisk. Three arguments are currently supported, --no-countdown,
  8. # --no-prompt and --no-stop-now-first
  9. LOGFILE=/var/log/asterisk/safe_asterisk_restart.log
  10. ASTERISK=/usr/sbin/asterisk
  11. SAFE_ASTERISK=/usr/sbin/safe_asterisk
  12. DELAY=1 # Seconds between steps in countdown
  13. COUNTDOWN_FROM=5 # Steps to count down
  14. DO_COUNTDOWN=1 # Should I do a countdown before restarting asterisk?
  15. DO_PROMPT=1 # Should I prompt the user?
  16. TRY_STOP_NOW_FIRST=1 # Attempt a 'stop now' before killing processes. Note
  17. # that this might make this script hang if asterisk
  18. # can't respond to the command.
  19. # processes to kill. Please list all AGI scripts here as well as the asterisk
  20. # processes, since asterisk may leave them unkilled.
  21. PROCVICTIMS="safe_asterisk asterisk mpg123"
  22. # helper functions
  23. # die ["string to print"]
  24. function die {
  25. if [[ "$1" != "" ]]; then
  26. echo $1
  27. else
  28. echo "ok. no harm done..."
  29. fi
  30. exit
  31. }
  32. # docmd "string to print" "cmd"
  33. function docmd {
  34. printf "$1..."
  35. `$2 >> $LOGFILE 2>&1`
  36. RETCODE=$?
  37. sleep $DELAY
  38. if [[ "$RETCODE" == "0" ]]; then
  39. echo " OK"
  40. else
  41. echo " FAILED"
  42. fi
  43. }
  44. # prompt "string" "positive answer"
  45. function prompt {
  46. printf "$1"
  47. read answer
  48. if [[ "$answer" != "$2" ]]; then
  49. die
  50. fi
  51. }
  52. # countdown secs
  53. function countdown {
  54. echo -n "$1 "
  55. if [[ $1 > 0 ]]; then
  56. sleep 1
  57. countdown $[ $1 - 1 ]
  58. else
  59. echo "boom!"
  60. fi
  61. }
  62. # am I really root?
  63. if [[ "$UID" != "0" ]]; then
  64. echo "Sorry, only root can do this." >&2
  65. exit;
  66. fi
  67. echo "`date`: $0 invoked" >> $LOGFILE
  68. # bash
  69. for i
  70. do
  71. if [[ "$i" == "--no-countdown" ]]
  72. then
  73. unset DO_COUNTDOWN
  74. fi
  75. if [[ "$i" == "--no-prompt" ]]
  76. then
  77. unset DO_PROMPT
  78. fi
  79. if [[ "$i" == "--no-stop-now-first" ]]
  80. then
  81. unset TRY_STOP_NOW_FIRST
  82. fi
  83. done
  84. [[ $DO_PROMPT ]] && prompt "Are you sure you want to restart asterisk? (yes/no)? " "yes"
  85. [[ $DO_PROMPT ]] && prompt "Really sure? (yes/no)? " "yes"
  86. [[ $DO_PROMPT ]] && prompt "Absolutely positive? (YES/no)? " "YES"
  87. [[ $DO_COUNTDOWN ]] && echo "OK, I'll do it, but if you're not sure about this, press ctrl+c now."
  88. [[ $DO_COUNTDOWN ]] && countdown $COUNTDOWN_FROM
  89. # doing the dirty work
  90. [[ $TRY_STOP_NOW_FIRST ]] && docmd "Asking asterisk kindly to shutdown" "$ASTERISK -rx 'stop now'"
  91. docmd "Sending asterisk processes the TERM signal" "pkill -15 $PROCVICTIMS"
  92. docmd "Sending asterisk processes KILL signal" "pkill -9 $PROCVICTIMS"
  93. docmd "Starting safe_asterisk" "$SAFE_ASTERISK"
  94. for i in $PROCVICTIMS
  95. do
  96. ps axf | grep -w $i | grep -v grep
  97. done