Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6773199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:38:22+00:00 2026-05-26T15:38:22+00:00

I am using Groovy to execute commands on my Linux box and get back

  • 0

I am using Groovy to execute commands on my Linux box and get back the output, but I am not able to use | pipes somehow (I think) or maybe it is not waiting for the command to finish.

What is wrong or what am I missing in my code?

My calling function:

def test()
{
    String result="N"

    HashMap<String,String> params = IntermediateResults.get("userparams")
    Map env=AppContext.get(AppCtxProperties.environmentVariables)

    def fClass = new GroovyClassLoader().parseClass( new File( 'plugins/infa9/Infa9CommandExecUtil.groovy' ) )
    List<String> frows=["uname -a",
                        "uname -a | awk '{print\$2}'",
                        "uname -a | cut -d ' ' -f 2"]
    List<String> resultRows = fClass.newInstance().fetchCommandOutput( params, env, frows )

    return result
}

Infa9CommandExecUtil.groovy file content (update: added exitVal println):

package infa9

import java.io.BufferedReader;

public class Infa9CommandExecUtil {
  StringBuffer result

  public Infa9CommandExecUtil() {
    result = new StringBuffer()
  }

  public List<String> fetchCommandOutput( Map<String,String> params, Map env, List<String> rows )
  {

        List<String> outputRows = new ArrayList<String>()
        try
        {
            for(item in rows)
            {
                String temp=item.toString()
                println "CMD:$temp"
                Process proc = Runtime.getRuntime().exec(temp);
                InputStream stdin = proc.getInputStream();
                InputStreamReader isr = new InputStreamReader(stdin);
                BufferedReader br = new BufferedReader(isr);
                String line = null;

                result = new StringBuffer()
                line=null

                int exitVal = proc.waitFor()    //do I need to wait for the thread/process to finish here?

                while ((line = br.readLine()) != null)
                {
                    result.append(line+System.getProperty("line.separator"))    //to maintain the format (newlines)
                }
                String tRes=result
                tRes=tRes.trim()
                println "OUTPUT:$tRes\nEXITVAL:$exitVal"

                outputRows.add(tRes)
            }
        }
        catch (IOException io)  {   io.printStackTrace();}
        catch (InterruptedException ie) {ie.printStackTrace();}
    return  outputRows
  }
}

My output (update: added exitVal value):

CMD:uname -a
OUTPUT:Linux estilo 2.6.18-128.el5 #1 SMP Wed Dec 17 11:41:38 EST 2008 x86_64 x86_64 x86_64 GNU/Linux
EXITVAL:0
CMD:uname -a | awk '{print$2}'
OUTPUT:
EXITVAL:1
CMD:uname -a | cut -d ' ' -f 2
OUTPUT:
EXITVAL:1

Note: I am internally using sh -c <command>.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:38:22+00:00Added an answer on May 26, 2026 at 3:38 pm

    You cannot do pipes or redirects using String.execute(). This doesn’t work in Java, so it doesn’t work in Groovy either…

    You can use Process.pipeTo with Groovy to simplify things:

    Process proca = 'uname -a'.execute()
    Process procb = 'awk {print\$2}'.execute()
    
    (proca | procb).text
    

    A more generic version could be:

    String process = 'uname -a | awk {print\$2}'
    
    // Split the string into sections based on |
    // And pipe the results together
    Process result = process.tokenize( '|' ).inject( null ) { p, c ->
      if( p )
        p | c.execute()
      else
        c.execute()
    }
    // Print out the output and error streams
    result.waitForProcessOutput( System.out, System.out )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a java project (not using groovy) but I'd like to interactively play
I'm able to run Groovy scripts from Groovy using proc = cmd /c groovy
How can I use the Groovy Console to kick off junit tests? (Currently using
I am using Groovy Sql to fetch results. This is the output from my
I'm using groovy.xml.MarkupBuilder to create XML response but it creates prettyprinted result which is
Using groovy, would you expect better performance in terms of speed and memory overhead
I've been using Groovy for all of five hours and just came across the
I am working on a project using Groovy, and I would like to take
From the book Groovy and Grails recipes I'm using the following code snippet: String
Using PyObjC , you can use Python to write Cocoa applications for OS X.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.