Asterisk LogoTrying to debug an AGI in Asterisk can be a frustrating experience. The main problem is that STDERR only outputs to the terminal Asterisk was started from. In most cases, this means the terminal is the server console. If your Asterisk server is sitting in the room you are coding in, that's no problem. But, if your Asterisk server is 2,000 miles away, this goes beyond annoyance.

GNU screen to the rescue. For those that don't know, screen allows you to run virtual terminals on most UNIX-like operating systems. What's more, you can detach from the virtual terminal (returning you to your regular shell) and re-attach to that session from another location. Meanwhile, screen acts as if nothing has happened.

If you use screen to start your Asterisk server, you will always have access to the controlling console, and thus, your debugging output. I use the following syntax when starting my Asterisk server:

/usr/bin/screen -L -d -m /usr/sbin/asterisk -vvvgc

The '-L' tells screen to keep a log of the session. This may be cumbersome if your Asterisk server is extremely busy. '-d -m' starts screen in detached mode. In other words, the command is executed on the virtual terminal and then the session detaches. This is extremely important in the case of a startup script. Finally, /usr/sbin/asterisk -vvvgc starts the Asterisk server with verbosity level set to 3, unlimited core size, and a console on the virtual terminal.

Once you have Asterisk running in a virtual terminal via screen. Use screen -r to re-attach to the session. Now all output from your AGIs will be logged to the terminal. When you're done, use Ctl-a Ctl-d to detach. Refer to the screen man page for more details on the operation of screen.

For more information on AGI programming, see Nir Simionovich's book, Asterisk Gateway Interface 1.4 and 1.6 Programming.