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