Note: You will need to start the Zaptel drivers as well if you are using Zaptel hardware. See Asterisk Fedora Core 3 for details on loading the drivers on boot.
Note: Most of the documentation and examples you will see are in environments where Asterisk is installed and run as the root user of the machine.
If you have just installed Asterisk, you can try running it for the first time in console mode with some debugging applied with this command:
/usr/sbin/asterisk -vvvgc
If it does not start, check the hardware requirements.
See Asterisk CLI for available commands. Use the command stop now to shut down Asterisk.
Command Line Parameters
Running Asterisk is actually rather straight forward. Asterisk, if run with no arguments, is launched as a daemon process:
/usr/sbin/asterisk
You can get a CLI console to an already-running daemon by typing
/usr/sbin/asterisk -r
on the same computer on which Asterisk is running. More than one console CLI can connect to Asterisk simultaneously.
Here are the available command line parameters:
- h : Help. Run ‘/sbin/asterisk -h’ to get a list of the available command line parameters.
- C <configfile>: Starts Asterisk with a different configuration file than the default /etc/asterisk/asterisk.conf.
- f : Foreground. Starts Asterisk but does not fork as a background daemon.
- c : Enables console mode. Starts Asterisk in the foreground (implies -f), with a console command line interface (CLI) that can be used to issue commands and view the state of the system.
- r : Remote console. Starts a CLI console which connects to an instance of Asterisk already running on this machine as a background daemon.
- R : Remote console. Starts a CLI console which connects to an instance of Asterisk already running on this machine as a background daemon and attempts to reconnect if disconnected.
- t : Record soundfiles in /var/tmp and move them where they belong after they are done.
- T : Display the time in “Mmm dd hh:mm:ss” format for each line of output to the CLI.
- n : Disable console colorization (for use with -c or -r)
- i: Prompt for cryptographic initialization passcodes at startup.
- p : Run as pseudo-realtime thread. Run with a real-time priority. (Whatever that means.)
- q : Quiet mode (supress output)
- v : Increase verbosity (multiple v’s = more verbose)
- V : Display version number and exit.
- d : Enable extra debugging across all modules.
- g : Makes Asterisk dump core in the case of a segmentation violation.
- G <group> : Run as a group other than the caller.
- U <user> : Run as a user other than the caller
- x <cmd> : Execute command <cmd> (only valid with -r)
Starting Asterisk at Boot Time
Different operating systems have slightly different methods of starting programs at boot time. The /usr/src/asterisk/contrib/init.d directory contains some sample scripts for some operating systems. Choose one or write your own to suit your particular installation.
You can choose either to launch /sbin/asterisk directly, or make use of the shell script installed as /sbin/safe_asterisk which launches Asterisk and attempts to relaunch Asterisk if it crashes.
For systems which still use inittab, one very simple way to start asterisk at boot and restart it if it crashes, is by adding the following line to /etc/inittab:
as:2:respawn:/usr/sbin/asterisk -f
The ‘2’ in the entry above is the run level(s) in which you wish the asterisk process to run. You can specify multiple run levels, see man inittab for details. Many Debian systems default to run level 2, so setting asterisk to start in runlevel 2 via inittab will result in it always starting at boot time and also being respawned if the process dies for any reason. There may be reasons to prefer a boot script to inittab, and your mileage may vary, but this works well for me.
If you are using an Ubuntu system (6.06 and newer), inittab has been deprecated in favor of “upstart”. To start Asterisk with upstart, create/edit the asterisk startup file:
$ sudo vi /etc/event.d/asterisk
Then put the contents of the following in the file:
- This service maintains Asterisk from the point the system is
- started until it is shut down again.
description “Asterisk daemon”
start on runlevel-2
stop on shutdown
respawn
exec //usr/sbin/asterisk -f
If you are using a Red Hat environment, you might be able to get a working rc.d/init.d script by typing:
cd /usr/src/asterisk
make config
This copies the script from contrib/init.d/rc.redhat.asterisk as the file /etc/rc.d/init.d/asterisk and then runs
/sbin/chkconfig –add asterisk
Starting/Stopping Zaptel as a Linux service
With Debian (and probably others), you can get a script to stop/start zaptel:
cd /usr/src/zaptel
make config
This will put zaptel in your init.d directory. For Debian, you can use update-rc.d to start at boot.
Advanced RC script for RedHat/CentOS
– extended Status and stop functions
#!/bin/bash
#
# taken from asterisk 1.2.6
#
# $Id: asterisk,v 1.3 2005/11/17 22:30:01 Gregory Boehnlein <[email protected]>
#
# asterisk Starts, Stops and Reloads Asterisk.
#
# chkconfig: 2345 40 60
# description: Asterisk PBX and telephony daemon.
# processname: asterisk
# pidfile: /var/run/asterisk.pid
#
# Thu Nov 17 2005 Gregory Boehnlein <[email protected]>
# - Updated Version to 1.3
# - Reversed behavior of LD_ASSUME_KERNEL=2.4.1
# - Added detailed failure messages
#
# Sun Jul 18 2004 Gregory Boehnlein <[email protected]>
# - Updated Version to 1.2
# - Added test for safe_asterisk
# - Verified SIGTERM issued by "killproc" ensures "stop gracefully"
# - Added support for -U and -G command line options
# - Modified "reload" to call asterisk -rx 'reload'
# Use this option to specify a different configuration directory
#AST_CONFIG=/etc/asterisk
# Installation directory
AST_SBIN=/usr/sbin
# Uncomment the following and set them to the user/groups that you
# want to run Asterisk as. NOTE: this requires substantial work to
# be sure that Asterisk's environment has permission to write the
# files required for its operation, including logs, its comm
# socket, the asterisk database, etc.
AST_USER="asterisk" # der naechste der diese auskommentiert
AST_GROUP="asterisk" # wird um einen kopf kuerzer!!!
# Source function library.
. /etc/rc.d/init.d/functions
if ! [ -x $AST_SBIN/asterisk ] ; then
echo "ERROR: /usr/sbin/asterisk not found"
exit 0
fi
if ! [ -d $AST_CONFIG ] ; then
echo "ERROR: /etc/asterisk directory not found"
exit 0
fi
# Uncomment this ONLY if you know what you are doing.
##rt LD_ASSUME_KERNEL=2.4.1
# Full path to asterisk binary
DAEMON=$AST_SBIN/asterisk
AST_BIN=$DAEMON
# Full path to safe_asterisk script
SAFE_ASTERISK=$AST_SBIN/safe_asterisk
# Allow configuration overrides in /etc/sysconfig/asterisk
CONFIG0=`readlink $0`
if [ "$CONFIG0" = "" ]; then
CONFIGFILE=/etc/sysconfig/`basename $0`
else
CONFIGFILE=/etc/sysconfig/`basename $CONFIG0`
fi
[ -x $CONFIGFILE ] && . $CONFIGFILE
RETVAL=0
start() {
###adjust file/directory permissions on each startup
echo "Initiating Start of Asterisk: "
echo -n "Adjusting file/directory permissions: "
. /etc/rc.d/init.d/asterisk-permissions && echo "OK" || echo "FAILED"
echo -n "Setting ulimits: "
ulimit -n 32768 -c unlimited && echo "OK" || echo "FAILED"
# Start daemons.
echo -n $"Starting asterisk: "
if [ -f $SAFE_ASTERISK ] ; then
DAEMON=$SAFE_ASTERISK
fi
if [ $AST_USER ] ; then
ASTARGS="-U $AST_USER"
fi
if [ $AST_GROUP ] ; then
ASTARGS="$ASTARGS -G $AST_GROUP"
fi
if [ $AST_CONFIG ]; then
ASTARGS="$ASTARGS -C $AST_CONFIG"
fi
### here we go!
daemon $DAEMON $ASTARGS
RETVAL=$?
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/asterisk
echo
echo -n "decrease the verbosity level to zero: "
$DAEMON -rx 'set verbose 0' > /dev/null &2>1 && echo "OK" || echo "FAILED"
return $RETVAL
}
stop() {
# Stop daemons.
echo "Shutting down Asterisk: "
## this is the nice way to stop asterisk ... initiate the shutdown from itself
## deactivated because its suspected to disturb the logger .... 6.6.07
# if [ ! "`pgrep asterisk`" ]; then echo "already stopped!"; exit 0; fi
# for ast_stop_cmd in "stop when convenient" "stop gracefully" "stop now"; do
# if [ ! "`pgrep asterisk`" ]; then
# break # when theres no process skip the addtional ast-stop
# else # otherwise send asterisk the stop commands
# echo -n "executing command: $ast_stop_cmd "
# for each in `seq 1 10`; do
# $AST_BIN -rx "$ast_stop_cmd" > /dev/null 2>&1
# echo -n "."
# sleep 1
# if [ ! "`pgrep asterisk`" ]; then echo -n " stopped"; break; fi
# done
# echo
# fi
# done
killproc asterisk
skill asterisk
echo -n "Asterisk shutdown: "
if [ ! "`pgrep asterisk`" ]; then
success
else
failure
fi
echo
}
reload() {
$AST_BIN -rx 'reload' > /dev/null 2> /dev/null
}
status() {
RETVAL=0
echo -n "Checking Version: $AST_BIN: "
$AST_BIN -V
echo -n "Checking PID-file: "
if [ -e /var/run/asterisk/asterisk.pid ]; then
echo "OK - `cat /var/run/asterisk/asterisk.pid`"
else
echo "FAILED! /var/run/asterisk/asterisk.pid does not exist"
RETVAL=1
fi
echo -n "Checking Process: "
PIDS=`pgrep asterisk`
if [ "$PIDS" != "" ]; then
echo -n "OK -"; for each in $PIDS; do echo -n " $each"; done; echo
else
echo "FAILED! no process named asterisk found"
RETVAL=1
fi
echo -n "Checking Console: "
$AST_BIN -rx 'show warranty' > /dev/null; RET=$?
if [ "$RET" == "0" ] ; then
echo "OK"
else
echo "FAILED!"
fi
$AST_BIN -rcx 'show uptime' 2>&1 | grep uptime
$AST_BIN -rcx 'show channels' 2>&1 | grep active
return $RETVAL
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
$0 stop
sleep 1
$0 start
;;
reload)
reload
;;
condrestart)
[ -f /var/lock/subsys/asterisk ] && restart || :
;;
status)
status
;;
*)
echo "Usage: asterisk {start|stop|restart|reload|condrestart|status}"
exit 1
esac
exit $RETVAL
Advanced RC script using Monit, also Redhat/CentOS
Monit-config:
check process asterisk with pidfile /var/run/asterisk/asterisk.pid
start program = "/etc/init.d/asterisk asteriskstart | /usr/bin/logger -t asteriskrc"
stop program = "/etc/init.d/asterisk asteriskstop | /usr/bin/logger -t asteriskrc"
if 5 restarts within 5 cycles then timeout
alert [email protected]
Asterisk rc:
# first of all, lets define the monit stuff
MONIT=/usr/local/bin/monit
MONITRC=/usr/local/etc/monitrc
# we want to be sure that the config file is OK
chown root:root $MONITRC
chmod 0700 $MONITRC
#
# taken from asterisk 1.2.6
#
# $Id: asterisk,v 1.3 2005/11/17 22:30:01 Gregory Boehnlein <[email protected]>
#
# asterisk Starts, Stops and Reloads Asterisk.
#
# chkconfig: 2345 40 60
# description: Asterisk PBX and telephony daemon.
# processname: asterisk
# pidfile: /var/run/asterisk.pid
#
# Thu Nov 17 2005 Gregory Boehnlein <[email protected]>
# - Updated Version to 1.3
# - Reversed behavior of LD_ASSUME_KERNEL=2.4.1
# - Added detailed failure messages
#
# Sun Jul 18 2004 Gregory Boehnlein <[email protected]>
# - Updated Version to 1.2
# - Added test for safe_asterisk
# - Verified SIGTERM issued by "killproc" ensures "stop gracefully"
# - Added support for -U and -G command line options
# - Modified "reload" to call asterisk -rx 'reload'
# Use this option to specify a different configuration directory
#AST_CONFIG=/etc/asterisk
# Installation directory
AST_SBIN=/usr/sbin
# Uncomment the following and set them to the user/groups that you
# want to run Asterisk as. NOTE: this requires substantial work to
# be sure that Asterisk's environment has permission to write the
# files required for its operation, including logs, its comm
# socket, the asterisk database, etc.
AST_USER="asterisk" # der naechste der diese auskommentiert
AST_GROUP="asterisk" # wird um einen kopf kuerzer!!!
# Source function library.
. /etc/rc.d/init.d/functions
if ! [ -x $AST_SBIN/asterisk ] ; then
echo "ERROR: /usr/sbin/asterisk not found"
exit 0
fi
if ! [ -d $AST_CONFIG ] ; then
echo "ERROR: /etc/asterisk directory not found"
exit 0
fi
# Uncomment this ONLY if you know what you are doing.
##rt LD_ASSUME_KERNEL=2.4.1
# Full path to asterisk binary
DAEMON=$AST_SBIN/asterisk
AST_BIN=$DAEMON
# Full path to safe_asterisk script
SAFE_ASTERISK=$AST_SBIN/safe_asterisk
# Allow configuration overrides in /etc/sysconfig/asterisk
CONFIG0=`readlink $0`
if [ "$CONFIG0" = "" ]; then
CONFIGFILE=/etc/sysconfig/`basename $0`
else
CONFIGFILE=/etc/sysconfig/`basename $CONFIG0`
fi
[ -x $CONFIGFILE ] && . $CONFIGFILE
RETVAL=0
############################################################################################
asterisk_start() {
###adjust file/directory permissions on each startup
echo "Initiating Start of Asterisk: "
echo -n "Adjusting file/directory permissions: "
. /etc/rc.d/init.d/asterisk-setpermissions && echo "OK" || echo "FAILED"
echo -n "Setting ulimits: "
ulimit -n 32768 -c unlimited && echo "OK" || echo "FAILED"
# Start daemons.
echo -n $"Starting asterisk: "
if [ -f $SAFE_ASTERISK ] ; then
DAEMON=$SAFE_ASTERISK
fi
if [ $AST_USER ] ; then
ASTARGS="-U $AST_USER"
fi
if [ $AST_GROUP ] ; then
ASTARGS="$ASTARGS -G $AST_GROUP"
fi
if [ $AST_CONFIG ]; then
ASTARGS="$ASTARGS -C $AST_CONFIG"
fi
### here we go!
daemon $DAEMON $ASTARGS
RETVAL=$?
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/asterisk
echo
return $RETVAL
}
#############################################################################################
asterisk_stop() {
# Stop daemons.
echo "Shutting down Asterisk: "
# this is the nice way to stop asterisk ... initiate the shutdown from itself
# deactivated because its suspected to disturb the logger .... 6.6.07
if [ ! "`pgrep asterisk`" ]; then echo "already stopped!"; exit 0; fi
for ast_stop_cmd in "stop when convenient" "stop gracefully" "stop now"; do
if [ ! "`pgrep asterisk`" ]; then
break # when theres no process skip the addtional ast-stop
else # otherwise send asterisk the stop commands
echo -n "executing command: $ast_stop_cmd "
for each in `seq 1 3`; do
$AST_BIN -rx "$ast_stop_cmd" > /dev/null 2>&1
echo -n "."
sleep 0.5
if [ ! "`pgrep asterisk`" ]; then echo -n " stopped"; break; fi
done
echo
fi
done
killproc $AST_BIN
skill $AST_BIN
echo -n "Asterisk shutdown: "
if [ ! "`pgrep asterisk`" ]; then
echo "successfully stopped"
else
echo "FAIL!"
fi
echo
return $RETVAL
}
#############################################################################################
reload() {
echo -n "send '$reload_string' to asterisk: "
$AST_BIN -rnx 'reload' > /dev/null 2>&1 ; RET=$?
if [ "$RET" == "0" ] ; then
echo "OK"
else
echo "FAILED!"
RETVAL=1
fi
return $RETVAL
}
#############################################################################################
status() {
RETVAL=0
echo -n "Checking Version: $AST_BIN: "
$AST_BIN -V
echo -n "Checking PID-file: "
if [ -e /var/run/asterisk/asterisk.pid ]; then
echo "OK - `cat /var/run/asterisk/asterisk.pid`"
else
echo "FAILED! /var/run/asterisk/asterisk.pid does not exist"
RETVAL=1
fi
echo -n "Checking Process: "
PIDS=`pidof $AST_BIN`
if [ "$PIDS" != "" ]; then
echo -n "OK -"; for each in $PIDS; do echo -n " $each"; done; echo
else
echo "FAILED! no process named asterisk found"
RETVAL=1
fi
echo -n "Checking Console: "
$AST_BIN -rnx 'show warranty' > /dev/null 2>&1 ; RET=$?
if [ "$RET" == "0" ] ; then
echo "OK"
else
echo "FAILED!"
RETVAL=1
fi
$AST_BIN -rnx 'show uptime' 2>&1 | strings | grep uptime
$AST_BIN -rnx 'show channels' 2>&1 | strings | grep active
return $RETVAL
}
#############################################################################################
monit_start() {
echo -n "Start Asterisk through monit... "
$MONIT -c $MONITRC start asterisk
RETVAL=$?
if [ "$RETVAL" == "0" ] ; then
echo "success"
else
echo "FAILED!"
fi
for each in `seq 1 10`; do sleep 0.5; echo -n "."; done; echo
echo -n "decrease the verbosity level to zero: "
$DAEMON -rx 'set verbose 0' > /dev/null &2>1 && echo "OK" || echo "FAILED"
status
}
#############################################################################################
monit_stop() {
echo -n "Stop Asterisk through monit... "
$MONIT -c $MONITRC stop asterisk
RETVAL=$?
if [ "$RETVAL" == "0" ] ; then
echo "success"
else
echo "FAILED!"
fi
for each in `seq 1 10`; do sleep 0.5; echo -n "."; done; echo
status
}
#############################################################################################
# See how we were called.
case "$1" in
start)
monit_start
;;
stop)
monit_stop
;;
asteriskstart)
asterisk_start | logger -t asteriskrc
;;
asteriskstop)
asterisk_stop | logger -t asteriskrc
;;
restart)
$0 stop
echo -n "waiting 5 seconds before start "
for each in `seq 1 10`; do sleep 0.5; echo -n "."; done; echo
echo
$0 start
;;
reload)
reload_string="reload $2"
reload
;;
condrestart)
[ -f /var/lock/subsys/asterisk ] && restart || :
;;
status)
status
;;
*)
echo "Usage: $0 {start|stop|restart|reload|condrestart|status}
start starts asterisk through monit
stop stops asterisk through monit
asteriskstart starts asterisk direct (thats what monit does)
asteriskstop stops asterisk direct (thats what monit does)
restart stop, wait 5sec, start
reload send 'reload' to asterisk. can be combined with subsystem (e.g. reload moh)
condrestart restarts only if asterisk was running before
status gives a status overview
"
exit 1
esac
exit $RETVAL