asterisk asterisk-addons astierks-mysql freepbx mysql Ubuntu

Final Asterisk clean up

This time I’m going to go through the final pieces of the Asterisk clean up on my Ubuntu 10.04 setup.

There were two annyoing things left behind by my install and setup of Asterisk.

  1. When I shutdown freepbx (aka amportal) the Asterisk instance would crash and then restart
  2. Ubuntu would want to update asterisk-mysql package

I’m going to show you how I got around these two issues. Firstly the amportal startup string and how to kill that nasty Asterisk crash and core dump.

The script I use to startup freepbx is below

# Provides: asterisk
# Required-Start: \$network \$syslog
# Required-Stop: \$network \$syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Asterisk daemon.
# Description: This script handles start/stop states of asterisk.

set -e
set -a

#test -x $DAEMON || exit 0

d_start() {
amportal start
d_stop() {
amportal stop
d_reload() {
amportal restart
case "$1" in
echo -n "Starting $DESC: $NAME"
echo "."
echo -n "Stopping $DESC: $NAME"
echo "."
echo -n "Restarting $DESC: $NAME"
sleep 10
echo "."
echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
exit 3
exit 0

Save this to /etc/init.d/amportal and then do the following

update_rc.d amportal defaults 80 20
update_rc.d -f asterisk remove

so that FreePBX will come up when you boot and remove the Asterisk from starting on its own. You could also use the sysv-rc-conf tool which allows you to do this visually.
You notice that this runs the amportal script which is located at /usr/local/sbin. Inside of there andother script is invoke called freepbx_engine. This is typically located at /var/lib/asterisk/bin. Inside there you will see lines like

killall -9 safe_asterisk
/usr/sbin/safe_asterisk -U asterisk ...
/usr/sbin/asterisk -rx "core stop gracefully" | grep -v "No such command"
/usr/sbin/asterisk -rx "stop gracefully" | grep -v -E "No such command|deprecated"

What is Safe_Asterisk? This is a script which starts a loop and runs Asterisk inside the loop. If Asterisk was to die for some reason then it detects this and restarts Asterisk.

There are two problems which I had with this. Firstly safe_asterisk starts Asterisk in the foreground, so my startup script would appear to hang after Asterisk started. Second when I killed the script (by using Ctrl+C), Asterisk would stop and crash and then restart and sometimes it would fail to restart and keep re-trying.

Getting Asterisk to start in the background was simple enough, but the crashes and attempted restarts still happened when I shutdown freepbx.

The solution I found was to do utilize the existing Asterisk startup script. I replaced the above lines with this

#killall -9 safe_asterisk
killall -9 asterisk
#/usr/sbin/safe_asterisk -U asterisk -G $AMPASTERISKGROUP
/etc/init.d/asterisk start
#/usr/sbin/asterisk -rx "core stop gracefully" | grep -v "No such command"
#/usr/sbin/asterisk -rx "stop gracefully" | grep -v -E "No such command|deprecated"
/etc/init.d/asterisk stop

That solved all my problems with the starting and stopping of freepbx.

Now, if you’ve added on the Asterisk Addon from my previous post. You may find that next time you do a apt-get or aptitude to update your OS that it wants to pull down an “updated” asterisk-mysql. Do not do this! It will overwrite the addons and they will then not load. To keep from being prompted you need to pin this package at the version that its on and you will not be prompted anymore. This is thanks to this page.

echo asterisk-mysql hold | dpkg --set-selections

and to check that worked you do

dpkg --get-selections | grep asterisk

asterisk install
asterisk-config install
asterisk-mysql hold
asterisk-sounds-extra install
asterisk-sounds-main install

what more could a man want!

I hope that helps a lot more of you with using FreePBX which just gets better and better.