<html>
<head>
    <base href="https://wiki.asterisk.org/wiki">
            <link rel="stylesheet" href="/wiki/s/en/2172/18/9/_/styles/combined.css?spaceKey=AST&amp;forWysiwyg=true" type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://wiki.asterisk.org/wiki/display/AST/Hangup+Handlers">Hangup Handlers</a></h2>
    <h4>Page  <b>added</b> by             <a href="https://wiki.asterisk.org/wiki/display/~mjordan">Matt Jordan</a>
    </h4>
         <br/>
    <div class="notificationGreySide">
         <h2><a name="HangupHandlers-Overview"></a>Overview </h2>

<p>Hangup handlers allow attaching subroutines to a channel that will execute when that channel hangs up.  Unlike the traditional <a href="/wiki/display/AST/Handling+Special+Extensions" title="Handling Special Extensions">h extension</a>, hangup handlers follow the channel, so regardless of where in the dialplan a channel is executing, if that channel is hung up, the hangup handlers will be run.</p>

<p>Multiple hangup handlers can be attached to a single channel.  If multiple hangup handlers are attached to a channel, the hangup handlers will be executed in the order of most recently added first.</p>

<h3><a name="HangupHandlers-Notes"></a>Notes</h3>
<ul>
        <li>Please note that when the hangup handlers execute in relation to the h extension is not defined.  They could execute before or after the h extension.</li>
        <li>Call transfers, call pickup, and call parking can result in channels on both sides of a bridge containing hangup handlers.</li>
        <li>Hangup handlers can be attached to any call leg using pre-dial routines.</li>
</ul>


<div class='panelMacro'><table class='warningMacro'><colgroup><col width='24'><col></colgroup><tr><td valign='top'><img src="/wiki/images/icons/emoticons/forbidden.gif" width="16" height="16" align="absmiddle" alt="" border="0"></td><td><b>WARNINGS</b><br /><ul>
        <li>As hangup handlers are subroutines, they must be terminated with a call to <a href="/wiki/display/AST/Asterisk+11+Application_Return" title="Asterisk 11 Application_Return">Return</a>.</li>
        <li>Adding a hangup handler in the h extension or during a hangup handler execution is undefined behaviour.</li>
        <li>As always, hangup handlers, like the h extension, need to execute quickly because they are in the hangup sequence path of the call leg.  Specific channel driver protocols like ISDN and SIP may not be able to handle excessive delays completing the hangup sequence.</li>
</ul>
</td></tr></table></div>

<h2><a name="HangupHandlers-DialplanApplicationsandFunctions"></a>Dialplan Applications and Functions</h2>

<p>All manipulation of a channel's hangup handlers are done using the <a href="/wiki/display/AST/Asterisk+11+Function_CHANNEL" title="Asterisk 11 Function_CHANNEL">CHANNEL</a> function.  The CHANNEL values that can be manipulated are:</p>

<ul>
        <li><b>hangup_handler_push</b> - used to push a hangup handler onto a channel</li>
        <li><b>hangup_handler_pop</b> - used to pop a hangup handler off a channel.  Optionally, a replacement hangup handler can be added to the channel.</li>
        <li><b>hangup_handler_wipe</b> - remove all hangup handlers on the channel.  Optionally, a new hangup handler can be pushed onto the channel.</li>
</ul>


<h5><a name="HangupHandlers-ExampleAddinghanguphandlerstoachannel"></a>Example - Adding hangup handlers to a channel</h5>

<p>In this example, three hangup handlers are added to a channel: hdlr3, hdlr2, and hdlr1.  When the channel is hung up, they will be executed in the order of most recently added first - so hdlr1 will execute first, followed by hdlr2, then hdlr3.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: java; gutter: false">; Some dialplan extension
same =&gt; n,Set(CHANNEL(hangup_handler_push)=hdlr3,s,1(args));
same =&gt; n,Set(CHANNEL(hangup_handler_push)=hdlr2,s,1(args));
same =&gt; n,Set(CHANNEL(hangup_handler_push)=hdlr1,s,1(args));
; Continuing in some dialplan extension

[hdlr1]

exten =&gt; s,1,Verbose(0, Executed First)
same =&gt; n,Return()

[hdlr2]

exten =&gt; s,1,Verbose(0, Executed Second)
same =&gt; n,Return()

[hdlr3]

exten =&gt; s,1,Verbose(0, Executed Third)
same =&gt; n,Return()</pre>
</div></div>

<h5><a name="HangupHandlers-ExampleRemovingandreplacinghanguphandlers"></a>Example - Removing and replacing hangup handlers</h5>

<p>In this example, three hangup handlers are added to a channel: hdlr3, hdlr2, and hdlr1.  Using the <a href="/wiki/display/AST/Asterisk+11+Function_CHANNEL" title="Asterisk 11 Function_CHANNEL">CHANNEL</a> function's <b>hangup_handler_pop</b> value, hdlr1 is removed from the stack of hangup handlers.  Then, using the <b>hangup_handler_pop</b> value again, hdlr2 is replaced with hdlr4.  When the channel is hung up, hdlr4 will be executed, followed by hdlr3.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: java; gutter: false">; Some dialplan extension
same =&gt; n,Set(CHANNEL(hangup_handler_push)=hdlr3,s,1(args));
same =&gt; n,Set(CHANNEL(hangup_handler_push)=hdlr2,s,1(args));
same =&gt; n,Set(CHANNEL(hangup_handler_push)=hdlr1,s,1(args));
; Remove hdlr1
same =&gt; n,Set(CHANNEL(hangup_handler_pop))
; Replace hdlr2 with hdlr4
same =&gt; n,Set(CHANNEL(hangup_handler_pop)=hdlr4,s,1(args));

; Continuing in some dialplan extension

[hdlr1]

exten =&gt; s,1,Verbose(0, Not Executed)
same =&gt; n,Return()

[hdlr2]

exten =&gt; s,1,Verbose(0, Not Executed)
same =&gt; n,Return()

[hdlr3]

exten =&gt; s,1,Verbose(0, Executed Second)
same =&gt; n,Return()

[hdlr4]

exten =&gt; s,1,Verbose(0, Executed First)
same =&gt; n,Return()</pre>
</div></div>
    </div>
    <div id="commentsSection" class="wiki-content pageSection">
       <div style="float: right;" class="grey">
                        <a href="https://wiki.asterisk.org/wiki/users/removespacenotification.action?spaceKey=AST">Stop watching space</a>
            <span style="padding: 0px 5px;">|</span>
                <a href="https://wiki.asterisk.org/wiki/users/editmyemailsettings.action">Change email notification preferences</a>
</div>
       <a href="https://wiki.asterisk.org/wiki/display/AST/Hangup+Handlers">View Online</a>
              |
       <a href="https://wiki.asterisk.org/wiki/display/AST/Hangup+Handlers?showComments=true&amp;showCommentArea=true#addcomment">Add Comment</a>
           </div>
</div>
</div>
</div>
</div>
</body>
</html>