#!/bin/bash ROOT_UID=0 # root uid is 0 E_NOTROOT=67 # Non-root exit error echo # check to see if we are root if [ "$UID" -ne "$ROOT_UID" ] then echo "Sorry, you must be root to run this script." echo exit $E_NOTROOT fi # make sure config file exists if [ ! -e "/etc/amportal.conf" ] # Check if file exists. then echo; echo "/etc/amportal.conf does not exist!"; echo "Have you installed the AMP configuration?"; exit; fi source /etc/amportal.conf chown_asterisk() { echo SETTING FILE PERMISSIONS chown -R asterisk:asterisk /var/run/asterisk chown -R asterisk:asterisk /etc/asterisk chown -R asterisk:asterisk /var/lib/asterisk chown -R asterisk:asterisk /var/log/asterisk chown -R asterisk:asterisk /var/spool/asterisk if [ -d /dev/zap ] then chown -R asterisk:asterisk /dev/zap fi chown asterisk /dev/tty9 chown -R asterisk:asterisk $AMPWEBROOT/admin chown -R asterisk:asterisk $FOPWEBROOT chown -R asterisk:asterisk $AMPWEBROOT/recordings chown -R asterisk:asterisk $AMPWEBROOT/_asterisk chown -R asterisk:asterisk $AMPCGIBIN/vmail.cgi chmod u+x /var/lib/asterisk/bin/*.pl chmod u+x /var/lib/asterisk/bin/archive_recordings chmod u+x /var/lib/asterisk/bin/retrieve_conf chmod u+x /var/lib/asterisk/agi-bin/* chmod u+x $AMPWEBROOT/admin/bounce_op.sh chmod u+x $AMPCGIBIN/vmail.cgi chmod u+x $FOPWEBROOT/*.pl chmod u+x $FOPWEBROOT/safe_opserver echo Permissions OK } check_asterisk() { # check to see if asterisk is running # 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 pid_length=`pidof asterisk|awk '{print length($0)}'` if [ "$pid_length" == "0" -a "$pid_length" != "" ] then killall -9 safe_asterisk killall -9 mpg123 > /dev/null echo echo "-----------------------------------------------------" echo "Asterisk could not start!" echo "Use 'tail /var/log/asterisk/full' to find out why." echo "-----------------------------------------------------" exit 0 fi } run_asterisk() { # check to see if asterisk is running echo echo "STARTING ASTERISK" pid_length=`pidof asterisk|awk '{print length($0)}'` if [ "$pid_length" != "0" -a "$pid_length" != "" ] then echo "Asterisk is already running" else # su - asterisk -c "export PATH=$PATH:/usr/sbin && export LD_LIBRARY_PATH=/usr/local/lib && /usr/sbin/safe_asterisk" export LD_LIBRARY_PATH=/usr/local/lib /usr/sbin/safe_asterisk -U asterisk -G asterisk sleep 5 check_asterisk sleep 1 check_asterisk echo "Asterisk Started" fi } stop_asterisk() { echo echo "STOPPING ASTERISK" pid_length=`pidof asterisk|awk '{print length($0)}'` if [ "$pid_length" != "0" -a "$pid_length" != "" ] then /usr/sbin/asterisk -rx "stop gracefully" echo "Asterisk Stopped" fi } check_fop() { #check to see if FOP is running pid_length=`pidof -x op_server.pl|awk '{print length($0)}'` if [ "$pid_length" == "0" -a "$pid_length" != "" ] then killall -9 safe_opserver echo echo "-----------------------------------------------------" echo "The FOP's server (op_server.pl) could not start!" echo "Please correct this problem" echo "-----------------------------------------------------" exit 0 fi } run_fop() { # check to see if FOP is running echo echo "STARTING FOP SERVER" pid_length=`pidof -x op_server.pl|awk '{print length($0)}'` if [ "$pid_length" != "0" -a "$pid_length" != "" ] then echo "FOP server is already running" else su - asterisk -c "cd $FOPWEBROOT && $FOPWEBROOT/safe_opserver &" sleep 1 check_fop echo "FOP Server Started" fi } stop_fop() { echo echo "STOPPING FOP SERVER" pid_length=`pidof -x op_server.pl|awk '{print length($0)}'` if [ "$pid_length" != "0" -a "$pid_length" != "" ] then killall -9 safe_opserver killall -9 op_server.pl echo "FOP Server Stopped" fi } kill_amp() { echo echo "KILLING AMP PROCESSES" killall -9 safe_asterisk killall -9 asterisk killall -9 mpg123 killall -9 safe_opserver killall -9 op_server.pl } case "$1" in start) chown_asterisk run_asterisk run_fop ;; stop) stop_asterisk stop_fop ;; restart) stop_asterisk stop_fop sleep 1 chown_asterisk run_asterisk run_fop ;; chown) chown_asterisk ;; kill) kill_amp ;; *) echo "----------AMP Control Script-----------" echo "Usage: amportal start|stop|kill|chown" echo echo "start: Starts Asterisk and Flash Operator Panel server" echo "stop: Gracefully stops Asterisk and the FOP server" echo "restart: Stop and Starts" echo "kill: Kills Asterisk and the FOP server" echo "chown: Sets appropriate permissions on files" echo exit 1 ;; esac