def METHOD_NAME():
    args = parse_args()
    # Set config defaults
    defaults = {
        'username': nav.config.NAV_CONFIG['NAV_USER'],
        'delay': '30',
        'mailwarnlevel': 'ERROR',
        'mailserver': 'localhost',
        'mailaddr': nav.config.NAV_CONFIG['ADMIN_MAIL'],
        'fromaddr': nav.config.NAV_CONFIG['DEFAULT_FROM_EMAIL'],
    }
    # Read config file
    config = nav.config.getconfig(configfile, defaults)
    # Set variables based on config
    username = config['main']['username']
    delay = int(config['main']['delay'])
    mailwarnlevel = config['main']['mailwarnlevel']
    if mailwarnlevel.isdigit():
        mailwarnlevel = int(mailwarnlevel)
    else:
        mailwarnlevel = getattr(logging, mailwarnlevel)
    mailserver = config['main']['mailserver']
    mailaddr = config['main']['mailaddr']
    fromaddr = config['main']['fromaddr']
    # Switch user to $NAV_USER (navcron) (only works if we're root)
    if os.geteuid() == 0 and not args.test:
        try:
            nav.daemon.switchuser(username)
        except nav.daemon.DaemonError as err:
            print(err, file=sys.stderr)
            print(
                "Run as root or %s. Try `%s --help' for more information."
                % (username, sys.argv[0]),
                file=sys.stderr,
            )
            sys.exit(1)
    # Initialize logger
    nav.logs.init_stderr_logging()
    # Init SMTP logging of grave errors
    if not loginitsmtp(mailwarnlevel, mailaddr, fromaddr, mailserver):
        sys.exit(1)
    # Check if already running
    try:
        nav.daemon.justme(pidfile)
    except nav.daemon.DaemonError as error:
        _logger.error(error)
        sys.exit(1)
    # Daemonize
    if not args.test and not args.foreground:
        try:
            nav.daemon.daemonize(pidfile, stderr=open(logfile, "a"))
        except nav.daemon.DaemonError as error:
            _logger.error(error)
            sys.exit(1)
        # Reopen log files on SIGHUP
        signal.signal(signal.SIGHUP, signalhandler)
    else:
        nav.daemon.writepidfile(pidfile)
    # Log reception of SIGTERM/SIGINT before quitting
    signal.signal(signal.SIGTERM, signalhandler)
    signal.signal(signal.SIGINT, signalhandler)
    # Loop forever
    _logger.info('Starting alertengine loop.')
    while True:
        try:
            check_alerts(debug=args.test)
            # nav.db connections are currently not in autocommit mode, and
            # since the current auth code uses legacy db connections we need to
            # be sure that we end all and any transactions so that we don't
            # idle.
            nav.db.commit_all_connections()
        except DatabaseError as err:
            _logger.error(
                'Database error, closing the DB connection just in ' 'case:\n%s', err
            )
            _logger.debug('', exc_info=True)
            if connection.queries:
                _logger.debug(connection.queries[-1]['sql'])
            try:
                connection.close()
            except InterfaceError:
                connection.connection = None
        except Exception as err:
            _logger.critical('Unhandled error: %s', err, exc_info=True)
            sys.exit(1)
        # Devel only
        if args.test:
            break
        else:
            # Sleep a bit before the next run
            _logger.debug('Sleeping for %d seconds.', delay)
            time.sleep(delay)
    # Exit nicely
    sys.exit(0)