Asterisk automatic daily restart

To automatically restart Asterisk you can add something like this to cron

  1. Restart Asterisk PBX once a day to prevent any problems from piling up
10 7 * * * root /usr/sbin/asterisk -rx "restart now" >/dev/null 2>&1


10 7 * * * root /usr/sbin/asterisk -r -x "restart gracefully" >/dev/null 2>&1

Please note that restarting asterisk is for the paranoid, and generally not needed if running asterisk stable.

(I run several Asterisk Servers, all stable versions, since 1.0.X and all of them need to periodically restarted. They can usually go two months before my FXO become locked, or the asterisk process just spins out of control. The CLI is always very strange when this happens (tab complete stops working, etc.) I disagree that it is paranoid, and judging from my experience, if you don't do this in a production enviornment you will most likely regret it.)

Some systems also have problems with Digium TDM400 cards that stop answering calls and need to be reset periodically. A script like this, run periodically by cron, can be useful:

  1. !/bin/sh

  1. This script tell asterisk to stop when there are no active calls,
  2. waits for it to actually stop, then reloads the wctdm module
  3. and restarts asterisk.

/usr/sbin/asterisk -rx "stop when convenient"
while /bin/ps ax | /bin/grep '[s]afe_asterisk' >/dev/null; do sleep 5; done
/sbin/rmmod wctdm
/sbin/modprobe wctdm

Here is another script. If you are absolutely paranoid that your Asterisk may not come back up, or you simply want an email notifying you that it went down, this script will take care of that for you (you could have email sent to your cell phone if your provider supports it, if you like).

  1. !/bin/sh
asterisk -rx "stop gracefully"
echo "Asterisk stopped at `date +%Y%m%d-%H%M%S`" | mail -s "Asterisk Stopped `date +%Y%m%d`" -c "" -b "" your@email.address
while [ -e "/var/run/" ] ; do sleep 1 ; done
rmmod wcfxs
rmmod zaptel
modprobe wcfxs
asterisk -p
while [ ! -e "/var/run/" ] ; do sleep 1 ; done
echo "Asterisk started at `date +%Y%m%d-%H%M%S`" | mail -s "Asterisk Started `date +%Y%m%d`" -c "" -b "" your@email.address
exit 0

Starting Asterisk with a cron-job script like above ends up with a zombie process (<defunct>).
Use the following line in your cron-sript and everything will work fine

/etc/init.d/asterisk start > /dev/null 2>&1 &

Created by: jht2, Last modification: Fri 14 of Oct, 2011 (18:48) by afu
Please update this page with new information, just login and click on the "Edit" or "Discussion" tab. Get a free login here: Register Thanks! - Find us on Google+