Starting Asterisk Without Scripts

Asterisk can be loaded in a variety of ways. The easiest way is to start Asterisk by running the binary file directly from the Linux command-line interface. If you are running a system that uses the init.d scripts, you can easily start and restart Asterisk that way as well. However, the preferred way of starting Asterisk is via the safe_asterisk script.

Console Commands

The Asterisk binary is, by default, located at /usr/sbin/asterisk. If you run /usr/sbin/asterisk, it will be loaded as a daemon. There are also a few switches you should be aware of that allow you to (re)connect to the Asterisk CLI, set the verbosity of CLI output, and allow core dumps if Asterisk crashes (for debugging with gdb). To explore the full range of options, run Asterisk with the -h switch:

# /usr/sbin/asterisk -h       

Here is a list of the most commonly used options:

-c

Console. This will start Asterisk as a user process (not as a server), and will connect you to the Asterisk CLI. This option is good when you are debugging your startup parameters, but should not be used for a normal system (if Asterisk is already running, this option will not work and will issue a complaint).

-v

Verbosity. This is used to set the amount of output for CLI debugging. The more “v”s, the more verbose.

-g

Core dump. If Asterisk were to crash unexpectedly, this would cause a core file to be created for later tracing with gdb. You generally do not use this in production, unless you are writing code for Asterisk and want to debug any resulting crashes.

-r

Remote. This is used to reconnect remotely to an already running Asterisk process. (The process is remote from the standpoint of the console connecting to it but is actually a local process on the machine. This has nothing to do with connecting to a remote process over a network using a protocol such as IP, as this is not supported.) This is the most common option and it is what you would use to connect to Asterisk on a system where it is running as a daemon/service that was started by init at boot time.

-x "<CLI command>"

Execute. Using this command in combination with -r allows you to execute a CLI command without having to connect to the CLI and type it manually. An example would be to send a restart, which you would do by typing asterisk -rx "reload" from the command line.

Let’s look at some examples. If you want to start Asterisk as a user program (because you are tweaking your config and will be starting and stopping it several times), and you want a verbosity level of 3, use the following command:

# /usr/sbin/asterisk -cvvv      

If the Asterisk process is already running (for example, if you have installed Asterisk as part of the init process of the system), use the reconnect switch, like so:

# /usr/sbin/asterisk -vvvr    

If you want Asterisk to dump a core file after a crash, you can use the -g switch when starting Asterisk:

# /usr/sbin/asterisk -g       

To execute a command without connecting to the CLI and typing it (perhaps for use within a script), you can use the -x switch in combination with the -r switch:

# /usr/sbin/asterisk -rx "restart now"
# /usr/sbin/asterisk -rx "database show"
# /usr/sbin/asterisk -rx "sip show peers"        

If you are experiencing crashes and would like to output to a debug file, use the following command:

# /usr/sbin/asterisk -vvvvc | tee /tmp/debug.log     

Note that you do not have to use the v switch if you do not want the system to provide detailed output of what is going on. On a busy system, you may not want to get any output, as it can interfere with whatever you are doing on the console.