def rundir(scriptdir, runtype, abort=False, submitscript=""):
    """Run scripts in directory scriptdir runtype is passed to the script if
    abort is True, a non-zero exit status will abort munki submitscript is put
    at the end of the scriptlist."""
    if os.path.exists(scriptdir):

        from munkilib import utils

        # Get timeout for scripts
        scriptTimeOut = 30
        if pref("scriptTimeOut"):
            scriptTimeOut = int(pref("scriptTimeOut"))
            display_detail("# Set custom script timeout to %s seconds" % scriptTimeOut)

        # Directory containing the scripts
        parentdir = os.path.basename(scriptdir)
        display_detail("# Executing scripts in %s" % parentdir)

        # Get all files in scriptdir
        files = os.listdir(scriptdir)

        # Sort files
        files.sort()

        # Find submit script and stick it on the end of the list
        if submitscript:
            try:
                sub = files.pop(files.index(submitscript))
                files.append(sub)
            except Exception, e:
                display_error("%s not found in %s" % (submitscript, parentdir))

        for script in files:

            # Skip files that start with a period
            if script.startswith("."):
                continue

            # Concatenate dir and filename
            scriptpath = os.path.join(scriptdir, script)

            # Skip directories
            if os.path.isdir(scriptpath):
                continue

            try:
                # Attempt to execute script
                display_detail("Running %s" % script)
                result, stdout, stderr = runExternalScriptWithTimeout(
                    scriptpath,
                    allow_insecure=False,
                    script_args=[runtype],
                    timeout=scriptTimeOut,
                )
                if stdout:
                    display_detail(stdout)
                if stderr:
                    display_detail("%s Error: %s" % (script, stderr))
                if result:
                    if abort:
                        display_detail("Aborted by %s" % script)
                        exit(1)
                    else:
                        display_warning("%s return code: %d" % (script, result))

            except utils.ScriptNotFoundError:
                pass  # Script has disappeared - pass.
            except Exception, e:
                display_warning("%s: %s" % (script, str(e)))