Hi all,
I have a strange problem with my Asterisk setup. I would like to use my PHP AGI script to catch the SIGHUP and do some cleanup (using pcntl_signal). My AGI script works perfectly on other Asterisk server with the same Asterisk version. Even on the same server everything is OK when I am sending the SIGHUP to the script manually, so this is not the problem of the PHP (PHP script is executable for asterisk and have #!/usr/bin/php on the first line). The same issue occurs with BASH script using “trap”. “AGISIGHUP” variable is not set (i have checked this before running the script in dialplan). Any ideas?
Asterisk version: 1.6.2.16.2
dialplan context:
[test-sighuptest]
exten => 123,1,AGI(/home/asterisk/public/agi/sighuptest.php)
exten => h,1,NoOp(hangup)
exten => h,n,Hangup
/home/asterisk/public/agi/sighuptest.php:
#!/usr/bin/php
<?php ################# End of call handler ################### function agi_hangup_handler($signo) { log_agi('SIGHUP received! Great!'); exit(0); } declare(ticks = 1); pcntl_signal(SIGHUP, "agi_hangup_handler"); ######################################################### function agi_exec($command) { global $separator; if (is_array($command)) $command=implode($separator, $command); fwrite(STDOUT, "$command\n"); fflush(STDOUT); $result = trim(fgets(STDIN)); $ret = array('code'=> -1, 'result'=> -1, 'timeout'=> false, 'data'=> ''); if (preg_match("/^([0-9]{1,3}) (.*)/", $result, $matches)) { $ret['raw'] = $result; $ret['code'] = $matches[1]; $ret['result'] = 0; if (preg_match('/^result=([0-9a-zA-Z-]*)\s?(?:\(?(.*?)\)?)?$/', $matches[2], $match)) { $ret['result'] = $match[1]; $ret['timeout'] = ($match[2] === 'timeout') ? true : false; $ret['data'] = $match[2]; } } return $ret; } function g2_1() { global $separator; $agivars = array(); while (!feof(STDIN)) { $agivar = trim(fgets(STDIN)); if ($agivar === '') break; else { $agivar = explode(':', $agivar); $agivars[$agivar[0]] = trim($agivar[1]); } } if (isset($agivars['agi_version'])) { $avr=explode('.',$agivars['agi_version'],3); $aver=$avr[0].$avr[1]; if ($aver < 14) $separator='|'; else $separator=','; } else $separator='|'; } function g2_answer(){ agi_exec("answer"); } function g2_playback($fil,$opt){ agi_exec(array("exec playback ".$fil, $opt)); } function g2_getvar($var) { $ret=agi_exec('get full variable ${'.$var.'}'); return $ret['data']; } function log_agi($entry, $level = 1) { if (!is_numeric($level)) { $level = 1; } agi_exec("VERBOSE \"$entry\" $level"); } g2_1(); g2_answer(); g2_playback('/home/asterisk/public/sounds/dn-test','noanswer'); $time_start = microtime_float(); for ($i=0; $i<2; $i++) { $clip=g2_getvar('CALLERID(num)'); } $time_end = microtime_float(); $time = $time_end - $time_start; log_agi('CLIP: '.$clip); log_agi("execution time: $time s"); g2_playback('/home/asterisk/public/sounds/dn-test','noanswer'); exit(0); ?>console debug (please note that call has been ended during the first playback):
– Goto (test-sighuptest,123,1)
– Executing [123@test-sighuptest:1] AGI(“SIP/test01-0000002f”, “/home/asterisk/public/agi/phpagig2test.php”) in new stack
– Launched AGI Script /home/asterisk/public/agi/phpagig2test.php
<SIP/test01-0000002f>AGI Tx >> agi_request: /home/asterisk/public/agi/phpagig2test.php
<SIP/test01-0000002f>AGI Tx >> agi_channel: SIP/test01-0000002f
<SIP/test01-0000002f>AGI Tx >> agi_language: en
<SIP/test01-0000002f>AGI Tx >> agi_type: SIP
<SIP/test01-0000002f>AGI Tx >> agi_uniqueid: 1303362755.49
<SIP/test01-0000002f>AGI Tx >> agi_version: 1.6.2.16.2
<SIP/test01-0000002f>AGI Tx >> agi_callerid: 999
<SIP/test01-0000002f>AGI Tx >> agi_calleridname: unknown
<SIP/test01-0000002f>AGI Tx >> agi_callingpres: 0
<SIP/test01-0000002f>AGI Tx >> agi_callingani2: 0
<SIP/test01-0000002f>AGI Tx >> agi_callington: 0
<SIP/test01-0000002f>AGI Tx >> agi_callingtns: 1
<SIP/test01-0000002f>AGI Tx >> agi_dnid: 123
<SIP/test01-0000002f>AGI Tx >> agi_rdnis: unknown
<SIP/test01-0000002f>AGI Tx >> agi_context: test-sighuptest
<SIP/test01-0000002f>AGI Tx >> agi_extension: 123
<SIP/test01-0000002f>AGI Tx >> agi_priority: 1
<SIP/test01-0000002f>AGI Tx >> agi_enhanced: 0.0
<SIP/test01-0000002f>AGI Tx >> agi_accountcode:
<SIP/test01-0000002f>AGI Tx >> agi_threadid: -1255146608
<SIP/test01-0000002f>AGI Tx >>
<SIP/test01-0000002f>AGI Rx << answer
<SIP/test01-0000002f>AGI Tx >> 200 result=0
<SIP/test01-0000002f>AGI Rx << exec playback /home/asterisk/public/sounds/dn-test,noanswer
– AGI Script Executing Application: (playback) Options: (/home/asterisk/public/sounds/dn-test,noanswer)
– <SIP/test01-0000002f> Playing ‘/home/asterisk/public/sounds/dn-test.slin’ (language ‘en’)
<SIP/test01-0000002f>AGI Tx >> 200 result=-1
<SIP/test01-0000002f>AGI Rx << get full variable ${CALLERID(num)}
<SIP/test01-0000002f>AGI Tx >> 200 result=1 (999)
<SIP/test01-0000002f>AGI Rx << get full variable ${CALLERID(num)}
<SIP/test01-0000002f>AGI Tx >> 200 result=1 (999)
<SIP/test01-0000002f>AGI Rx << VERBOSE “CLIP: 999” 1
/home/asterisk/public/agi/phpagig2test.php: CLIP: 999
<SIP/test01-0000002f>AGI Tx >> 200 result=1
<SIP/test01-0000002f>AGI Rx << VERBOSE “execution time: 0.00061607360839844 s” 1
/home/asterisk/public/agi/phpagig2test.php: execution time: 0.00061607360839844 s
<SIP/test01-0000002f>AGI Tx >> 200 result=1
<SIP/test01-0000002f>AGI Rx << exec playback /home/asterisk/public/sounds/dn-test,noanswer
– AGI Script Executing Application: (playback) Options: (/home/asterisk/public/sounds/dn-test,noanswer)
[2011-04-21 07:12:37.185] WARNING[6593]: file.c:753 ast_readaudio_callback: Failed to write frame
– <SIP/test01-0000002f> Playing ‘/home/asterisk/public/sounds/dn-test.slin’ (language ‘en’)
[2011-04-21 07:12:37.185] WARNING[6593]: app_playback.c:471 playback_exec: ast_streamfile failed on SIP/test01-0000002f for /home/asterisk/public/sounds/dn-test,noanswer
<SIP/test01-0000002f>AGI Tx >> 200 result=0
<SIP/test01-0000002f>AGI Rx << get full variable ${CALLERID(num)}
<SIP/test01-0000002f>AGI Tx >> 200 result=1 (999)
<SIP/test01-0000002f>AGI Rx << exec noop “A: 999”
– AGI Script Executing Application: (noop) Options: (A: 999)
<SIP/test01-0000002f>AGI Tx >> 200 result=0
<SIP/test01-0000002f>AGI Rx << VERBOSE “execution time: 0.00019001960754395 s” 1
/home/asterisk/public/agi/phpagig2test.php: execution time: 0.00019001960754395 s
<SIP/test01-0000002f>AGI Tx >> 200 result=1
– <SIP/test01-0000002f>AGI Script /home/asterisk/public/agi/phpagig2test.php completed, returning 0
– Executing [h@test-sighuptest:1] NoOp(“SIP/test01-0000002f”, “hangup”) in new stack
– Executing [h@test-sighuptest:2] Hangup(“SIP/test01-0000002f”, “”) in new stack
== Spawn extension (test-sighuptest, h, 2) exited non-zero on ‘SIP/test01-0000002f’