Bash Script – Move Asterisk Call Files into Spool Directory

After running into permission issues trying to get PHP’s shell_exec command to chown call files as the asterisk user(which only root can do), I decided to make this script.

As a side note, you should be able to get the shell_exec command to work using sudo by adding the www-data user to the sudoer’s file without a password but that wouldn’t work in my particular environment. The server I was working on was extremely outdated and didn’t even have sudo installed.

Add this to /etc/sudoers
www-data ALL=NOPASSWD: /path/to/script

The following code runs the script as a daemon. You will need to update rc to start this script at default run levels and also make sure to chmod +x this file to make it executable.

filename: /etc/init.d/mvcallfile

#!/bin/bash
# Move asterisk call file daemon startup script
# Author: Nathan Thomas

PROG=mvcallfile

DESC="Moves Asterisk call files to spool directory"
HOME_DIR=/usr/local/bin
DAEMON=$HOME_DIR/$PROG.sh
PIDFILE=/var/run/$PROG.pid

case "$1" in
	'start')
		# Remove old PIDFILE if it exists
		if [ -s $PIDFILE ] ; then
			rm -f $PIDFILE
		fi
		# Check for existing process
		ps_check=`ps aux | grep $PROG.sh | grep -v "grep $PROG.sh" | wc -l`
		if [ "$ps_check" -ge "1" ] ; then  # if count of proc >= 1
			echo "Cannot start $PROG, service is already running."
			pids=`pgrep $PROG.sh`
			echo "Try killing these processes first: $pids"
			exit 1
		else
			# Start the service
			echo "Starting $PROG service..."
			if start-stop-daemon --start --chuid root --pidfile $PIDFILE --chdir $HOME_DIR --background --make-pidfile --exec $DAEMON; then
				echo "OK"
			else
				echo "Error: Could not start $PROG service."
				exit 3
			fi
		fi
		;;
	'stop')
		echo "Stopping $PROG service..."
		if start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE; then	
			rm -f $PIDFILE
			echo "OK"
		else
			ps_check=`ps aux | grep $PROG.sh | grep -v "grep $PROG.sh" | wc -l`
			if [ "$ps_check" -eq "0" ] ; then
				echo "Error: Could not find an active $PROG process to kill."
				exit 1
			else
				pids=`pgrep $PROG.sh`
				echo "Error: Could not stop $PROG service. Try killing these processes manually: $pids"
				exit 1
			fi
		fi
		;;
	'restart')
		/etc/init.d/$PROG stop
		sleep 2
		/etc/init.d/$PROG start
		;;
	'status')
		ps_check=`ps aux | grep $PROG.sh | grep -v "grep $PROG.sh" | wc -l`
		echo "The $PROG service is..."
		if [ -s $PIDFILE -a "$ps_check" -ge "1" ] ; then  # if pidfile exists and size > 0 and # of procs >= 1
			echo "Running"
			exit 0
		else
			echo "Stopped"
			exit 1
		fi
		;;
	*)
		echo "Usage:  {start|stop|restart|status}"
		exit 1
		;;
esac
exit $?

This script does the actual moving of the call files and gets executed once a second. Make sure to change the call file and spool directory variables for your specific situation.

filename: /usr/local/bin/mvcallfile.sh

#!/bin/bash
# Take ownership and move asterisk call files to spool dir
# Author: Nathan Thomas

CF_DIR=/tmp
SPOOL_DIR=/var/spool/asterisk/outgoing

while true
do 
	if test -n "$(find $CF_DIR -maxdepth 1 -name '*.call' -print -quit)" ; then
		chown asterisk:asterisk $CF_DIR/*.call
		chmod 770 $CF_DIR/*.call
		mv $CF_DIR/*.call $SPOOL_DIR/
		sleep 1
	else
		sleep 1
	fi
done

Leave a Reply