Ticket #2646: freepbx_engine

File freepbx_engine, 7.3 kB (added by jfinstrom, 4 years ago)
Line 
1 #!/usr/bin/env bash
2
3 ROOT_UID=0   # root uid is 0
4 E_NOTROOT=67   # Non-root exit error
5
6
7 echo
8 # check to see if we are root
9 if [ "$UID" -ne "$ROOT_UID" ]
10 then
11   echo "Sorry, you must be root to run this script."
12   echo
13   exit $E_NOTROOT
14 fi
15
16 # make sure config file exists
17 if [ ! -e "/etc/amportal.conf" ]       # Check if file exists.
18   then
19     echo;
20     echo "/etc/amportal.conf does not exist!";
21   echo "Have you installed the AMP configuration?";
22   exit;
23 fi
24 . /etc/amportal.conf
25
26 if [ $ASTRUNDIR = /var/run ]
27   then
28     echo "**** ERROR IN CONFIGURATION ****"
29     echo "astrundir in /etc/asterisk/asterisk.conf is set to '/var/run' - THIS IS WRONG."
30     echo "Please change it to something sensible (eg, '/var/run/asterisk') and re-run"
31     echo "install_amp"
32     exit;
33 fi
34
35 if [ ! -d "$ASTRUNDIR" ]
36   then
37     echo "**** WARNING: ERROR IN CONFIGURATION ****"
38     echo "astrundir in /etc/asterisk/asterisk.conf is set to $ASTRUNDIR but the directory"
39     echo "does not exists. Attempting to create it with: 'mkdir -p $ASTRUNDIR'"
40     echo
41     mkdir -p $ASTRUNDIR
42     RET=$?
43     if [ $RET != 0 ]
44     then
45       echo "**** ERROR: COULD NOT CREATE $ASTRUNDIR ****"
46       echo "Attempt to execute 'mkdir -p $ASTRUNDIR' failed with an exit code of $RET"
47       echo "You must create this directory and the try again."
48       exit
49     fi
50 fi
51
52 chown_asterisk() {
53   echo SETTING FILE PERMISSIONS
54
55   chown -R asterisk:asterisk $ASTRUNDIR
56   chown -R asterisk:asterisk /etc/asterisk
57   chmod -R g+w /etc/asterisk
58   chown -R asterisk:asterisk $ASTVARLIBDIR
59   chmod -R g+w $ASTVARLIBDIR
60   chown -R asterisk:asterisk $ASTLOGDIR
61   chmod -R g+w $ASTLOGDIR
62   chown -R asterisk:asterisk $ASTSPOOLDIR
63   chmod -R g+w $ASTSPOOLDIR
64   chown -R asterisk:asterisk $AMPWEBROOT/admin
65   chmod -R g+w $AMPWEBROOT/admin
66   chown -R asterisk:asterisk $FOPWEBROOT
67   chmod -R g+w $FOPWEBROOT
68   chown -R asterisk:asterisk $AMPWEBROOT/recordings
69   chmod -R g+w $AMPWEBROOT/recordings
70   chown -R asterisk:asterisk $AMPWEBROOT/_asterisk
71   chmod u+x,g+x $ASTVARLIBDIR/bin/*
72   chown -R asterisk:asterisk $ASTVARLIBDIR/bin/*
73   chown -R asterisk:asterisk $AMPBIN/*
74
75   if [ "$ASTAGIDIR" != "" ]; then
76     chmod u+x $ASTAGIDIR/*
77   else
78     chmod u+x $ASTVARLIBDIR/agi-bin/*
79   fi
80
81   chmod u+x,g+x $AMPBIN/bounce_op.sh
82   chmod u+x,g+x $FOPWEBROOT/*.pl
83   chmod u+x $FOPWEBROOT/safe_opserver
84   chown asterisk /dev/tty9
85   # Ensure that various hardware devices are owned correctly.
86   [ -e /dev/zap ] && chown -R asterisk:asterisk /dev/zap
87   [ -e /dev/capi20 ] && chown -R asterisk:asterisk /dev/capi20
88   [ -e /dev/misdn ] && chown -R asterisk:asterisk /dev/misdn
89   [ -e /dev/mISDN ] && chown -R asterisk:asterisk /dev/mISDN
90   [ -e /dev/dsp ] && chown -R asterisk:asterisk /dev/dsp
91
92   echo Permissions OK
93 }
94
95 check_asterisk() {
96 # check to see if asterisk is running
97 # Note, this isn't fool-proof.  If safe_asterisk is constantly restarting a dying asterisk, then there is a chance pidof will return non zero.  We call this twice to reduce chances of this happening
98 pid_length=$(pidof asterisk|awk '{print length($0)}')
99   if [ "$pid_length" == "0" -a "$pid_length" != "" ]
100     then
101         killall -9 safe_asterisk
102         killall -9 mpg123 > /dev/null
103         echo
104         echo "-----------------------------------------------------"
105         echo "Asterisk could not start!"
106         echo "Use 'tail $ASTLOGDIR/full' to find out why."
107         echo "-----------------------------------------------------"
108         exit 0
109     fi
110 }
111 check_asterisk_kill() {
112 # check to see if asterisk is running after killall
113 # Note, this isn't fool-proof.  If safe_asterisk is constantly restarting a dying asterisk, then there is a chance pidof will return non zero.  We call this twice to reduce chances of this happening
114 pid_length=$(pidof asterisk|awk '{print length($0)}')
115   if [ "$pid_length" == "0" -a "$pid_length" != "" ]
116     then
117         killall -9 safe_asterisk
118         killall -9 asterisk
119         check_asterisk_kill
120     fi
121 }
122
123 run_asterisk() {
124 # check to see if asterisk is running
125 echo
126 echo "STARTING ASTERISK"
127 pid_length=$(pidof asterisk|awk '{print length($0)}')
128   if [ "$pid_length" != "0" -a "$pid_length" != "" ]
129     then
130       echo "Asterisk is already running"
131     else
132       # su - asterisk -c "export PATH=$PATH:/usr/sbin && export LD_LIBRARY_PATH=/usr/local/lib && /usr/sbin/safe_asterisk"
133       export LD_LIBRARY_PATH=/usr/local/lib
134       /usr/sbin/safe_asterisk -U asterisk -G asterisk
135       sleep 5
136       check_asterisk
137       sleep 1
138       check_asterisk
139       echo "Asterisk Started"
140     fi
141 }
142
143 stop_asterisk() {
144 echo
145 echo "STOPPING ASTERISK"
146 pid_length=$(pidof asterisk|awk '{print length($0)}')
147   if [ "$pid_length" != "0" -a "$pid_length" != "" ]
148     then
149       /usr/sbin/asterisk -rx "stop gracefully"
150       echo "Asterisk Stopped"
151     fi
152 }
153
154 check_fop() {
155 #check to see if FOP is running
156   pid_length=$(pidof -x op_server.pl|awk '{print length($0)}')
157   if [ "$pid_length" == "0" -a "$pid_length" != "" ]
158     then
159         ps -ef | grep safe_opserver | grep -v grep | awk '{print $2}' | xargs kill -9
160         echo
161         echo "-----------------------------------------------------"
162         echo "The FOP's server (op_server.pl) could not start!"
163         echo "Please correct this problem"
164         echo "-----------------------------------------------------"
165         exit 0
166     fi
167 }
168
169 run_fop() {
170 # check to see if FOP is running
171 echo
172 echo "STARTING FOP SERVER"
173 pid_length=$(pidof -x op_server.pl|awk '{print length($0)}')
174   if [ "$pid_length" != "0" -a "$pid_length" != "" ]
175     then
176       echo "FOP server is already running"
177     else
178       su - asterisk -c "cd $FOPWEBROOT && $FOPWEBROOT/safe_opserver &"
179       sleep 1
180       check_fop
181       echo "FOP Server Started"
182     fi
183 }
184
185 stop_fop() {
186   echo
187   echo "STOPPING FOP SERVER"
188   pid_length=$(pidof -x op_server.pl|awk '{print length($0)}')
189     if [ "$pid_length" != "0" -a "$pid_length" != "" ]
190       then
191         ps -ef | grep safe_opserver | grep -v grep | awk '{print $2}' | xargs kill
192         killall op_server.pl
193         echo "FOP Server Stopped"
194       fi
195 }
196
197 kill_amp() {
198   echo
199   echo "KILLING AMP PROCESSES"
200   killall -9 safe_asterisk
201   killall -9 asterisk
202   check_asterisk_kill
203   killall -9 mpg123
204   ps -ef | grep safe_opserver | grep -v grep | awk '{print $2}' | xargs kill -9
205   killall -9 op_server.pl
206 }
207
208 case "$1" in
209   start)
210     chown_asterisk
211     run_asterisk
212     if [ -z "$FOPRUN" -o "$FOPRUN" == "true" -o "$FOPRUN" == "TRUE" -o "$FOPRUN" == "True" -o "$FOPRUN" == "yes" -o "$FOPRUN" == "YES" -o "$FOPRUN" == "Yes" ]
213     then
214       run_fop
215     fi
216   ;;
217   stop)
218     stop_asterisk
219     stop_fop
220   ;;
221   restart)
222     stop_asterisk
223     stop_fop
224     sleep 1
225     chown_asterisk
226     run_asterisk
227     if [ -z "$FOPRUN" -o "$FOPRUN" == "true" -o "$FOPRUN" == "TRUE" -o "$FOPRUN" == "True" -o "$FOPRUN" == "yes" -o "$FOPRUN" == "YES" -o "$FOPRUN" == "Yes" ]
228     then
229       run_fop
230     fi
231   ;;
232   stop_fop)
233     stop_fop
234   ;;
235   start_fop)
236     run_asterisk
237     run_fop
238   ;;
239   restart_fop)
240     stop_fop
241     run_asterisk
242     run_fop
243   ;;
244   chown)
245     chown_asterisk
246   ;;
247   kill)
248     kill_amp
249   ;;
250   *)
251     echo "-------------FreePBX Control Script-----------------------------------------------"
252     echo
253     echo "Usage:       amportal start|stop|restart|start_fop|stop_fop|restart_fop|kill|chown"
254     echo
255     echo "start:       Starts Asterisk and Flash Operator Panel server if enabled"
256     echo "stop:        Gracefully stops Asterisk and the FOP server"
257     echo "restart:     Stop and Starts"
258     echo "start_fop:   Starts FOP server and Asterisk if not running"
259     echo "stop_fop:    Stops FOP serverg"
260     echo "restart_fop: Stops FOP server and Starts it and Asterisk if not running"
261     echo "kill:        Kills Asterisk and the FOP server"
262     echo "chown:       Sets appropriate permissions on files"
263     echo
264     exit 1
265   ;;
266 esac