2 # vim:textwidth=80:tabstop=4:shiftwidth=4:smartindent
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
10 LOGFILE
=/var
/log
/asterisk
/safe_asterisk_restart.log
11 ASTERISK
=/usr
/sbin
/asterisk
12 SAFE_ASTERISK
=/usr
/sbin
/safe_asterisk
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.
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"
27 # die ["string to print"]
29 if [[ "$1" != "" ]]; then
32 echo "ok. no harm done..."
37 # docmd "string to print" "cmd"
43 if [[ "$RETCODE" == "0" ]]; then
50 # prompt "string" "positive answer"
54 if [[ "$answer" != "$2" ]]; then
71 if [[ "$UID" != "0" ]]; then
72 echo "Sorry, only root can do this." >&2
76 echo "`date`: $0 invoked" >> $LOGFILE
81 if [[ "$i" == "--no-countdown" ]]
85 if [[ "$i" == "--no-prompt" ]]
89 if [[ "$i" == "--no-stop-now-first" ]]
91 unset TRY_STOP_NOW_FIRST
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"
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
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" "killall -15 $PROCVICTIMS"
105 docmd
"Sending asterisk processes KILL signal" "killall -9 $PROCVICTIMS"
106 docmd
"Starting safe_asterisk" "$SAFE_ASTERISK"
107 for i
in $PROCVICTIMS
109 ps axf |
grep -w $i |
grep -v grep