Groovy运行bash命令/脚本

/**
 * @author :ZackZhou
 * @date :Created in 2020/10/22 5:25 PM
 * @description :
 * @modified By:
 * @version:
 */
//approach 1
def command = ['bash','-c','ls;sleep 2; ls -lhst']
Process proc = new ProcessBuilder(command).start()
def s_out = new StringBuilder(), s_err = new StringBuilder()
proc.consumeProcessOutput(s_out, s_err)
proc.waitForOrKill(60000) // wait io finish, at most 1 min
println(sout.toString()) 

//approach 2
def commands = ['bash','-c','ls;sleep 2; ls -lhst']
println commands.execute().text

//execute scripts
def file = new File("./BashScript.sh")
def command_file
= file.text
println command_file.execute().text

 

posted @ 2020-10-26 10:22  ZackZhou  阅读(673)  评论(0编辑  收藏  举报