[TIP] Backround process, synchronous process

I struggled a long time to get a thread next to the active channel for synchronous operation with not much success.
This is the best I could come up with and I hope it will help others, so feel free to improve upon this.
I did try AGI and local dials amongst others, but each time as soon as a channel got answered, the other got terminated.

This was all done using Asterisk 13.2 on CentOS 7.0 and it works like this :
From the dialplan use system application to start a bash script.
Do not use shell function as this does not allow the bash script to start a program in the background.
The bash script starts a PhP program in the background using the ampersand. The bash script is later used to end the background process.
You want to stop the background processes when restarting Asterisk, when the channel hangs up or when the dialplan requires it to stop for some other reason.

To end all active background programs on “core restart now” add the following to the startup section of your dialplan :

For startup code have a look at :

To start a background program add the following to your dialplan where required :

${PARAM1}… are parameters to add at your discretion.

To stop a background program add the following to your dialplan where required and in your hangup section :

For hangup detection, use the h extension or a hangup handler.
wiki.asterisk.org/wiki/display/ … Extensions
the-asterisk-book.com/1.6/besond … sions.html
wiki.asterisk.org/wiki/display/ … p+Handlers

The bash script (yourbash) :

SN="${0##*/}" # Script name.
SD="${0:0:${#0}-${#SN}}" # Script location.

Begin background command.

if [ “$1” == “B” ]; then
$SD$SN.php $2 $3 $4 $5 $6 & > /dev/null 2>&1

End background command.

if [ “$1” == “E” ]; then

Look for yourbash.php started for this channel.

PID=ps ax|grep $SD$SN.php|grep -v grep|grep $2|awk '{print $1}'
if [ “$PID” != “” ]; then
kill -10 $PID

End ALL background commands.

if [ “$1” == “A” ]; then

Look for all yourbash.php started.

PID=ps ax|grep $SD$SN.php|grep -v grep|awk '{print $1}'
if [ “$PID” != “” ]; then
kill -9 $PID

The PhP script (yourbash.php) :


<?php declare (ticks = 1); // Global settings. $iTimer = 60; // Update interval. // Global variables. $CHANNEL = ''; $PARAM1 = 0; $PARAM2 = 0; $Hangup = false; $SIGEND = false; $tLast = 0; // Initializations. date_default_timezone_set ('GMT'); /* Functions. ******************************************************************/ // SIGnal handler function function sig_handler ($signo) { switch ($signo) { case SIGUSR1: $GLOBALS ['SIGEND'] = true; break; default: // handle all other signals } } pcntl_signal (SIGUSR1, "sig_handler"); /* Functions. ******************************************************************/ /* Load initialisation settings. ***********************************************/ // The script needs at least the CHANNEL, ... if (count ($argv) < 1) {exit;} $CHANNEL = $argv [1]; $PARAM1 = $argv [2]; $PARAM2 = $argv [3]; settype ($PARAM1, 'integer'); settype ($PARAM2, 'integer'); /* Load initialisation settings. ***********************************************/ /* Build background function. **************************************************/ // Keep running until your own reasons are met or until the script is killed by Asterisk. while (true) { // Remember last update time. $tLast = time (); // Check if channel is still active. $R = exec ('asterisk -rx "core show channels verbose"|grep "'.$CHANNEL.'"'); if ($R == "") { // Channel has terminated and we are still running, so quit. exit; } // Add code for your own use. // ... // Force hangup from this end. if ($Hangup) { $R = exec ('asterisk -rx "channel request hangup '.$CHANNEL.'"'); // Terminating code on forced hangup. // ... exit; } // Wait for next update. if (!$SIGEND) {sleep ($iTimer);} // Client has terminated the call. if ($SIGEND) { // Terminating code on hangup. // ... exit; } } /* Build background function. **************************************************/ ?>[/code]

I noticed a problem with the length of a channel name in the above code so I changed the line

$R = exec ('asterisk -rx "core show channels verbose"|grep "'.$CHANNEL.'"');


$R = exec ('asterisk -rx "core show channel '.$CHANNEL.'"|grep "'.$CHANNEL.'"|grep -v "is not a known channel"');