Running a script from Groovy
Asked Answered
V

4

6

In order to get my setup a bit closer to "one click deployment", I would like to use groovy scripts to start/stop other processes controlled by bat scripts, running in different parts of the filesystem and even on different machines.

How to execute these scripts and how to do it from their respective working directory?

I know Java's

java.lang.Runtime's exec()

However there are lots of issues with this and I wondered if Groovy had some kind of shorthand for this as well?

Thanks!

Varick answered 26/8, 2009 at 12:21 Comment(0)
S
11

Groovy added an execute() method to plain old String, so try this:

println "ls -la".execute().text
Safari answered 26/8, 2009 at 13:18 Comment(1)
you are a lucky one that "ls" is a stand-alone executable - and not a shell built in command.Repartition
W
9

The execute() method can be used to change directories if you prefix it with the "cmd /c" command, and then use ampersand (assuming Windows) to chain commands together.

Example, assuming you want to go to subdirectory subdir and run a couple of batch files from there:

println "cmd /c cd subdir & batch1.bat & batch2.bat".execute().text

Not sure if there isn't a better way, but this does work.

Wastrel answered 29/8, 2009 at 0:58 Comment(1)
for something similar on a Linux system you might need to do a variant like this: [ 'sh', '-c', 'your-commands-here'].execute().text - whilst on Windows systems command parsing of a single string is already in there under the hood (it might or might not be part of the api spec.)Repartition
M
1

You can also use ProcessBuilder which is a surprisingly convienent Java class introduced in java 5.

ProcessBuilder lets you

  • determine the working directory
  • determine which environmental variables the process should have

See http://download.oracle.com/javase/1.5.0/docs/api/java/lang/ProcessBuilder.html for a brief example and more documentation.

Merl answered 6/7, 2011 at 6:15 Comment(1)
I second that. Here's a blog post that combines Groovy and the ProcessBuilderDesilva
A
0

If you aren't afraid to create some reusable code you could create an object that wraps an .execute() process. I created something like this and use it regularly.

Create a new process with:

def proc="cmd".execute()

After that you can use "consumeProcessOutput()" to manage the input and output of "proc". Anything you send to it will be acted on as though you typed it into a shell, and all the output of that shell will be available to you.

I wrapped all this up in a closure so that you could do this:

cmd("cd \\ \n dir ") {
    if(it.contains("AUTOEXEC.BAT")) 
        println it;
    return true;
}

To get a display that shows only the autoexec.bat line. Note that until you return true from the closure, the stdin of that process is available so you can send more lines of text and interact with it indefinitely.

I use it quite a bit because commands like "cd" and "Dir" don't work in windows with .execute(), so a simple:

def directoryListing=cmd("cd\\\ndir")

will get me a quick directory listing with ease.

Aschim answered 12/12, 2012 at 20:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.