All pastes #58020 Raw Edit

Untitled

public text v1 · immutable
#58020 ·published 2006-05-22 14:06 UTC
rendered paste body
#!/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 $ASTRUNDIR
	chown -R asterisk:asterisk /etc/asterisk
	chmod -R g+w /etc/asterisk
	chown -R asterisk:asterisk $ASTVARLIBDIR
	chmod -R g+w $ASTVARLIBDIR
	chown -R asterisk:asterisk $ASTLOGDIR
	chmod -R g+w $ASTLOGDIR
	chown -R asterisk:asterisk $ASTSPOOLDIR
	chmod -R g+w $ASTSPOOLDIR
	chown -R asterisk:asterisk $AMPWEBROOT/admin
	chmod -R g+w $AMPWEBROOT/admin
	chown -R asterisk:asterisk $FOPWEBROOT
	chmod -R g+w $FOPWEBROOT
	chown -R asterisk:asterisk $AMPWEBROOT/recordings
	chmod -R g+w $AMPWEBROOT/recordings
	chown -R asterisk:asterisk $AMPWEBROOT/_asterisk
	chown -R asterisk:asterisk $AMPCGIBIN/vmail.cgi
	chmod u+x,g+x $ASTVARLIBDIR/bin/*
	chmod u+x $ASTVARLIBDIR/agi-bin/*
	chmod u+x,g+x $AMPWEBROOT/admin/bounce_op.sh
	chmod u+x,g+x $AMPCGIBIN/vmail.cgi
	chmod u+x,g+x $FOPWEBROOT/*.pl
	chmod u+x $FOPWEBROOT/safe_opserver
	chown asterisk /dev/tty9
	# Ensure that various hardware devices are owned correctly.
	[ -e /dev/zap ] && chown -R asterisk:asterisk /dev/zap 
	[ -e /dev/capi20 ] && chown -R asterisk:asterisk /dev/capi20
	[ -e /dev/misdn ] && chown -R asterisk:asterisk /dev/misdn
	[ -e /dev/dsp ] && chown -R asterisk:asterisk /dev/dsp

	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 $ASTLOGDIR/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