groovy (java): exec() do not detach from process (Intellij IDEA vs Maven+TestNG)

允我心安 提交于 2019-12-11 18:01:43

问题


I have Groovy Maven2 test project with TestNG and Surefire plugin enabled.

I want to launch external process (*.cmd file which start some *.exe file) in last test method, finish my tests and left process running after tests.

I tried the following codes to do it:

1 attempt

def builder = new ProcessBuilder('cmd','/c <name>.cmd')
builder.directory( ( new File( <path_to_working_directory> ) ) )
builder.start()

2 attempt (with and without start cmd option)

Runtime.getRuntime().exec( "cmd /c start <name>.cmd", null , ( new File( <path_to_working_directory> ) ) )

3 attempt

( new AntBuilder() ).exec(
    dir: "<path_to_working_directory>",
    executable: "<name>.cmd"
)     

Where .cmd is:

set path=<path_to_execFile>;%path%
start <execFileName>.exe

When I launch each of these codes from Intellij IDEA via 'Run' functionality (Alt+Shift+F10) codes execute successfully, process started and run after test finishes.

When I launch each of these codes both from Intellij IDEA Maven task, clean Maven installation (and even Maven task from Jenkins) process started successfully but test remains running. I need to kill it manually. When I kill test process (Maven process) manually my launched external process continue to work as I expect.

This hung test process is my headache for the moment.

I looked through a lot of materials but didn't find any root cause, fix and even workaround for this issue. I see that all my attempts (perhaps, except of AntBuilder()) create deattached processes. I suppose that this can be connected with JVM settings. But I coudnl't find to which one.

Also, I tried

"full command to run my cmd".execute()

but it didn't help me too.

Could you please help me resolve the issue?

Thanks In Advance!


回答1:


So, I do not see any answers for my issue here. But I have some updates.

I found that I can use PsExec tool instead of direct cmd calling:

def builder = new ProcessBuilder( 'psexec', 'cmd', '/c', '<name>.cmd' )
builder.directory( ( new File( <path_to_working_directory> ) ) )
builder.start()

And this code works fine when I launch it from clean Maven only (not from Jenkins): process started, Maven task completes successfully, the process continues to run.

But during execute this code as part of some Maven2 Jenkins task I faced to issue again: psexec started but Jenkins task is running and my process does not started before I terminate Jenkins task manually.

To avoid this issue I created simple additional Groovy service script I launch in listen mode (and Writing a TCP Server) on target machine manually during initial machine preparation. This script is running on machine always.

I send to this listener name of command file to execute from my test I launch from Jenkins and it executes all cmds successfully: processes start, Jenkins task completes successfully, processes continue to run. I use processbuilder inside this listener.

For name sending I use simple socket (Writing a TCP Client)

Also, I found how to detach child from process tree on win32?. But for me system my way looks more Groovy I think.



来源:https://stackoverflow.com/questions/17045593/groovy-java-exec-do-not-detach-from-process-intellij-idea-vs-maventestng

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!