Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a script from Groovy

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!

like image 533
raoulsson Avatar asked Aug 26 '09 12:08

raoulsson


People also ask

How do I run a Jenkins Groovy script?

Usage. To create Groovy-based project, add new free-style project and select "Execute Groovy script" in the Build section, select previously configured Groovy installation and then type your command, or specify your script file name. In the second case path taken is relatively from the project workspace directory.


2 Answers

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

println "ls -la".execute().text
like image 122
John Flinchbaugh Avatar answered Oct 21 '22 11:10

John Flinchbaugh


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.

like image 44
seansand Avatar answered Oct 21 '22 12:10

seansand