title: Asterisk AGI runs in console mode only
body: <p>I am trying to run a python script by calling it in the asterisk AGI Interface. I have had this running in a test environment but cannot seem to get this to run on Ubuntu 18.04.
  I can get the AGI script to run when I start asterisk with "sudo asterisk -vvgc"</p>
  
  <p>I have some of the script below to see if there may be a problem with logging and that is what is killing the script as it prints in the console view. </p>
  
  <p>Here is the AGI debug while asterisk runs in the background accessing the console via "sudo asterisk -vvvvvvr"</p>
  
  <pre><code>AGI Debugging Enabled
    == Using SIP RTP CoS mark 5
         &gt; 0x7f9288015b20 -- Strict RTP learning after remote address set to: 
  192.168.21.93:58654
  -- Executing [101@default:1] NoOp("SIP/1400-00000001", "Test User") in new 
  stack
  -- Executing [101@default:2] AGI("SIP/1400-00000001", "siptapeth.agi") in new 
  stack
  -- Launched AGI Script /var/lib/asterisk/agi-bin/siptapeth.agi
  &lt;SIP/1400-00000001&gt;AGI Tx &gt;&gt; agi_request: siptapeth.agi
  &lt;SIP/1400-00000001&gt;AGI Tx &gt;&gt; agi_channel: SIP/1400-00000001
  &lt;SIP/1400-00000001&gt;AGI Tx &gt;&gt; agi_language: en
  &lt;SIP/1400-00000001&gt;AGI Tx &gt;&gt; agi_type: SIP
  &lt;SIP/1400-00000001&gt;AGI Tx &gt;&gt; agi_uniqueid: 1531757391.1
  &lt;SIP/1400-00000001&gt;AGI Tx &gt;&gt; agi_version: 14.7.7
  &lt;SIP/1400-00000001&gt;AGI Tx &gt;&gt; agi_callerid: 1400
  &lt;SIP/1400-00000001&gt;AGI Tx &gt;&gt; agi_calleridname: Test User
  &lt;SIP/1400-00000001&gt;AGI Tx &gt;&gt; agi_callingpres: 0
  &lt;SIP/1400-00000001&gt;AGI Tx &gt;&gt; agi_callingani2: 0
  &lt;SIP/1400-00000001&gt;AGI Tx &gt;&gt; agi_callington: 0
  &lt;SIP/1400-00000001&gt;AGI Tx &gt;&gt; agi_callingtns: 0
  &lt;SIP/1400-00000001&gt;AGI Tx &gt;&gt; agi_dnid: 101
  &lt;SIP/1400-00000001&gt;AGI Tx &gt;&gt; agi_rdnis: unknown
  &lt;SIP/1400-00000001&gt;AGI Tx &gt;&gt; agi_context: default
  &lt;SIP/1400-00000001&gt;AGI Tx &gt;&gt; agi_extension: 101
  &lt;SIP/1400-00000001&gt;AGI Tx &gt;&gt; agi_priority: 2
  &lt;SIP/1400-00000001&gt;AGI Tx &gt;&gt; agi_enhanced: 0.0
  &lt;SIP/1400-00000001&gt;AGI Tx &gt;&gt; agi_accountcode: 
  &lt;SIP/1400-00000001&gt;AGI Tx &gt;&gt; agi_threadid: 140270109501184
  &lt;SIP/1400-00000001&gt;AGI Tx &gt;&gt; 
  -- &lt;SIP/1400-00000001&gt;AGI Script siptapeth.agi completed, returning 0
  -- Auto fallthrough, channel 'SIP/1400-00000001' status is 'UNKNOWN'
  </code></pre>
  
  <p>Here is the output when I run asterisk in console mode "sudo asterisk -vvgc"</p>
  
  <pre><code>`*CLI&gt; core set verbose 10
  Console verbose was 2 and is now 10.
  *CLI&gt; agi set debug on
  AGI Debugging Enabled
  *CLI&gt; agi set debug 10
  Usage: agi set debug [on|off]
     Enables/disables dumping of AGI transactions for
     debugging purposes.
  *CLI&gt;   == Using SIP RTP CoS mark 5
     &gt; 0x7ff3ac0091e0 -- Strict RTP learning after remote address set to: 
  192.168.21.93:59514
  -- Executing [101@default:1] NoOp("SIP/1400-00000000", "Test User") in new 
  stack
  -- Executing [101@default:2] AGI("SIP/1400-00000000", "siptapeth.agi") in new 
  stack
  -- Launched AGI Script /var/lib/asterisk/agi-bin/siptapeth.agi
  &lt;SIP/1400-00000000&gt;AGI Tx &gt;&gt; agi_request: siptapeth.agi
  &lt;SIP/1400-00000000&gt;AGI Tx &gt;&gt; agi_channel: SIP/1400-00000000
  &lt;SIP/1400-00000000&gt;AGI Tx &gt;&gt; agi_language: en
  &lt;SIP/1400-00000000&gt;AGI Tx &gt;&gt; agi_type: SIP
  &lt;SIP/1400-00000000&gt;AGI Tx &gt;&gt; agi_uniqueid: 1531758059.0
  &lt;SIP/1400-00000000&gt;AGI Tx &gt;&gt; agi_version: 14.7.7
  &lt;SIP/1400-00000000&gt;AGI Tx &gt;&gt; agi_callerid: 1400
  &lt;SIP/1400-00000000&gt;AGI Tx &gt;&gt; agi_calleridname: Test User
  &lt;SIP/1400-00000000&gt;AGI Tx &gt;&gt; agi_callingpres: 0
  &lt;SIP/1400-00000000&gt;AGI Tx &gt;&gt; agi_callingani2: 0
  &lt;SIP/1400-00000000&gt;AGI Tx &gt;&gt; agi_callington: 0
  &lt;SIP/1400-00000000&gt;AGI Tx &gt;&gt; agi_callingtns: 0
  &lt;SIP/1400-00000000&gt;AGI Tx &gt;&gt; agi_dnid: 101
  &lt;SIP/1400-00000000&gt;AGI Tx &gt;&gt; agi_rdnis: unknown
  &lt;SIP/1400-00000000&gt;AGI Tx &gt;&gt; agi_context: default
  &lt;SIP/1400-00000000&gt;AGI Tx &gt;&gt; agi_extension: 101
  &lt;SIP/1400-00000000&gt;AGI Tx &gt;&gt; agi_priority: 2
  &lt;SIP/1400-00000000&gt;AGI Tx &gt;&gt; agi_enhanced: 0.0
  &lt;SIP/1400-00000000&gt;AGI Tx &gt;&gt; agi_accountcode: 
  &lt;SIP/1400-00000000&gt;AGI Tx &gt;&gt; agi_threadid: 140687325234944
  &lt;SIP/1400-00000000&gt;AGI Tx &gt;&gt; 
  ARGS: ['/var/lib/asterisk/agi-bin/siptapeth.agi']
  ENV LINE: agi_request: siptapeth.agi
  ENV LINE: agi_channel: SIP/1400-00000000
  ENV LINE: agi_language: en
  ENV LINE: agi_type: SIP
  ENV LINE: agi_uniqueid: 1531758059.0
  ENV LINE: agi_version: 14.7.7
  ENV LINE: agi_callerid: 1400
  ENV LINE: agi_calleridname: Test User
  ENV LINE: agi_callingpres: 0
  ENV LINE: agi_callingani2: 0
  ENV LINE: agi_callington: 0
  ENV LINE: agi_callingtns: 0
  ENV LINE: agi_dnid: 101
  ENV LINE: agi_rdnis: unknown
  ENV LINE: agi_context: default
  ENV LINE: agi_extension: 101
  ENV LINE: agi_priority: 2
  ENV LINE: agi_enhanced: 0.0
  ENV LINE: agi_accountcode:
  ENV LINE: agi_threadid: 140687325234944
  ENV LINE: 
  class AGI: self.env = {'agi_accountcode': '',
   'agi_callerid': '1400',
   'agi_calleridname': 'Test User',
   'agi_callingani2': '0',
   'agi_callingpres': '0',
   'agi_callingtns': '0',
   'agi_callington': '0',
   'agi_channel': 'SIP/1400-00000000',
   'agi_context': 'default',
   'agi_dnid': '101',
   'agi_enhanced': '0.0',
   'agi_extension': '101',
   'agi_language': 'en',
   'agi_priority': '2',
   'agi_rdnis': 'unknown',
   'agi_request': 'siptapeth.agi',
   'agi_threadid': '140687325234944',
   'agi_type': 'SIP',
   'agi_uniqueid': '1531758059.0',
   'agi_version': '14.7.7'}
  INFO:axi:---Pin:101 - Message:Test User - checksum:410
  INFO:axi:b'211 Message sent.&amp;ltCR&amp;gt&amp;ltACK&amp;gt&amp;ltCR&amp;gt'
  INFO:axi:---Page Accepted - checksum:410
  -- &lt;SIP/1400-00000000&gt;AGI Script siptapeth.agi completed, returning 0
  -- Auto fallthrough, channel 'SIP/1400-00000000' status is 'UNKNOWN'`
  </code></pre>
  
  <p>Snippet of siptapeth.agi some of it was removed.</p>
  
  <pre><code>#!/usr/bin/python3
  
  import serial
  import re
  from time import sleep
  import logging
  import logging.handlers
  from configparser import ConfigParser
  import sys
  import asterisk
  import asterisk.agi
  from asterisk.agi import *
  import socket
  import os
  import sys
  
  config = ConfigParser()
  config.read('/var/www/html/config.ini')
  LOG_LEVEL = logging.info('LOGGING', 'level')
  
  # Initialize logging
  LOGGER = logging.getLogger('axi')
  LOGGER.setLevel(logging.INFO)
  formatter = logging.Formatter('|%(asctime)s|%(levelname)-8s|%(name)s|%        (message)s')
  log_file = logging.handlers.TimedRotatingFileHandler('/var/log/axi/input.csv',     when='midnight', backupCount=7)
  log_file.setLevel(logging.INFO)
  log_file.setFormatter(formatter)
  LOGGER.addHandler(log_file)
  
  # Only print to console if at DEBUG level
  if LOG_LEVEL == 'DEBUG':
  log_console = logging.StreamHandler()
  log_console.setLevel(logging.INFO)
  log_console.formatter(formatter)
  LOGGER.addHandler(log_console)
  
  BAUD = config.get('USB_Settings', 'baudrate')
  PTY = config.get('USB_Settings', 'parity')
  STPB = int(config.get('USB_Settings', 'stopbits'))
  BTSZ = int(config.get('USB_Settings', 'bytesize'))
  
  HOST = config.get('Ethernet_Paging_System', 'IP')
  PORT = config.get('Ethernet_Paging_System', 'Port')
  
  agi = AGI()
  
  pin = agi.env['agi_extension']
  msg = agi.env['agi_calleridname']
  
  #***SOME CODE REMOVED FOR SECURITY REASONS***
  
      #Socket setup
      s = None
      for res in socket.getaddrinfo(HOST, PORT, socket.AF_INET, socket.SOCK_STREAM):
      af, socktype, proto, canonname, sa = res
      try:
          s = socket.socket(af, socktype, proto)
      except OSError as msg:
          s = None
          continue
      try:
          s.connect(sa)
      except OSError as msg:
          s.close()
          s = None
          continue
      break
  if s is None:
      LOGGER.info('---Could not open socket')
      sys.exit(1)
  
  #***SOME CODE REMOVED FOR SECURITY REASONS***
  </code></pre>