5

I'm using Jenkins to launch a script on a linux machine.

When I run this manually on the server, it works:

/bin/bash -c '/some/script MyProduct SomeBranch'

When I run this with groovy, it doesn't work.

I get the same error as if I didn't pass the "-c" option, so somehow the "-c" isn't working.

Here is my code:

branchName = "SomeBranch"
configName = "release"
println "Building for branch "+branchName+" and configuration "+configName

def chkbranch = { String product, String branch -> mkcmd( product, branch ) } 
private def mkcmd ( String product, String branch ) {
  // Build the command string to run
      def cmd = "/bin/bash -c '/some/script "+product+" "+branch+"'"
      def sout = new StringBuffer()
      def serr = new StringBuffer()
  // Run the command
      println "running "+cmd
      def proc = cmd.execute()
      proc.consumeProcessOutput ( sout, serr )
      proc.waitForProcessOutput ()
      println "out> $sout"
      println "err> $serr"
      return sout
}

chkbranch ( "MyProduct", branchName )

Is this the correct way to build the command in Groovy?:

def cmd = "/bin/bash -c '/some/script "+product+" "+branch+"'"
cmd.execute()

Thanks!

Question similar to / helpful resources that I tried:

0

1 Answer 1

7

Try to run the command in the following way:

def cmd = ["/bin/bash", "-c", "/some/script", product, branch]

You may also try:

def cmd = ["/some/script", product, branch]

if /some/script is executable - BTW is it placed under root (/)?

Sign up to request clarification or add additional context in comments.

3 Comments

This worked! Now it's saying "permission denied" for accessing a file within the script, but it's running the script this time! Yes the script is in root, is there anything special I need to worry about for that?
Rather not. I thought about permissions :)
it must be ["/bin/bash", "-c", "/some/script $product $branch"]... - the shell command to run is the sole paramenter after -c. and take double care, what product and branch are. if product is <backtick>rm -rf /<backtick> it will run

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.