Joh*_*ker 6 shell groovy gradle
当使用 ctrl-c 终止 Gradle 构建时,如何终止在 cmd.execute()-call 中创建的进程。
在这个例子中,calcTask-task 启动了 shell 进程“longRunningProcess.bat”,它是一个“sleep 300”。当使用 ctrl+c 中止 Gradle 构建时,“睡眠”进程仍处于活动状态。该异常确实处理 shell 中的错误,但不处理 ctrl+c。
apply plugin: "groovy"
dependencies {
compile gradleApi()
}
task CreateLongRunningProcess(type: DefaultTask) {
def file = new File('longRunningProcess.bat')
file.write '@ECHO Start calculating - please wait 5 minutes.\n'
file << '@sleep 300\n'
file << '@ECHO Finished calculating.\n'
}
class CalcTask extends DefaultTask {
@TaskAction
def calculate() {
String cmd = './longRunningProcess.bat'
try {
def proc = cmd.execute()
proc.waitForProcessOutput(System.out, System.err)
} catch (Exception e) {
e.printStackTrace();
println 'Exception occured'
}
}
}
task calcTask(type: CalcTask) {
dependsOn(CreateLongRunningProcess)
}
Run Code Online (Sandbox Code Playgroud)
$ ps -ef | grep sleep
$ ./gradlew -q calcTask
Start calculating - please wait 5 minutes.
<Aborted with ctrl+c>
$ ps -ef | grep sleep
t035678 1900 1 ? 16:55:58 /usr/bin/sleep
$ kill 1900
$ ./longRunningProcess.bat
Start calculating - please wait 5 minutes.
<Aborted with ctrl+c>
Finished calculating.
$ ps -ef | grep sleep
$ ./gradlew --version
<snip>
------------------------------------------------------------
Gradle 4.4
------------------------------------------------------------
Build time: 2017-12-06 09:05:06 UTC
Revision: cf7821a6f79f8e2a598df21780e3ff7ce8db2b82
Groovy: 2.4.12
Ant: Apache Ant(TM) version 1.9.9 compiled on February 2 2017
JVM: 9.0.1 (Oracle Corporation 9.0.1+11)
OS: Windows 7 6.1 amd64
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1162 次 |
| 最近记录: |