use tzafrir's patch to fix this problem properly... i made the previous set of change...
[asterisk-bristuff.git] / contrib / scripts / safe_asterisk_restart
blob81783149a7fa8009ae3c20e913f100344d0a2368
1 #!/bin/bash
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"
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..."
34 exit
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"
50 # prompt "string" "positive answer"
51 function prompt {
52 printf "$1"
53 read answer
54 if [[ "$answer" != "$2" ]]; then
55 die
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!"
70 # am I really root?
71 if [[ "$UID" != "0" ]]; then
72 echo "Sorry, only root can do this." >&2
73 exit;
76 echo "`date`: $0 invoked" >> $LOGFILE
78 # bash
79 for i
81 if [[ "$i" == "--no-countdown" ]]
82 then
83 unset DO_COUNTDOWN
85 if [[ "$i" == "--no-prompt" ]]
86 then
87 unset DO_PROMPT
89 if [[ "$i" == "--no-stop-now-first" ]]
90 then
91 unset TRY_STOP_NOW_FIRST
93 done
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
110 done