Skip to content

Instantly share code, notes, and snippets.

@turboBasic
Last active August 3, 2021 10:27
Show Gist options
  • Save turboBasic/0aec0f223a7fcc5ed7db5fea091e0a54 to your computer and use it in GitHub Desktop.
Save turboBasic/0aec0f223a7fcc5ed7db5fea091e0a54 to your computer and use it in GitHub Desktop.
Execute long process and collect stdout and stderr in #groovy

Execute long process and collect stdout and stderr

Recipe 1

    def command = """
        java -Xms512m -Xmx1024m \
            -classpath test-suite-package-3.jar org.testng.TestNG \
            -testjar test-suite-package-3.jar \
            -d ./test-output-final/
    """
    Process proc = command.execute()

    OutputStream outputStream = new FileOutputStream('stdout.txt')
    OutputStream errorStream  = new FileOutputStream('stderror.txt')

    proc.waitForProcessOutput(outputStream, errorStream)

Recipe 2

    def proc = '/path/to/longRunningProcess'.execute(
        null, 
        new File('/path/to/workingDirectory')
    )

    new File('/path/to/output.txt').withWriter { outFile ->
        proc.in.eachLine { line ->
            def tokens = line.tokenize '\t'
            outFile.writeLine "${tokens.join(',')}"
        }
    }

    proc.waitFor()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment