1

I used to execute CMDs command inside python using subprocess, and my partial code is as follows.

import subprocess

proc = subprocess.Popen("mvn test", shell = True)
try:
    proc.wait(60)  # set timeout of proc
except subprocess.TimeoutExpired:
    proc.kill()  # kill the proc if it timed out
    print("subprocess is killed")

It do will kill the subprocess after proc.kill(), but the side-effect comes, Java(TM) Platform SE binary is still active and take much resource of my computer.So how can I finally kill the Java(TM) Platform SE binary?
I just supposed that command mvn test will call the JVM(something like that), when I kill the process proc, the Java(TM) Platform SE binary isn't be killed. Picture of Windows Resource Manager

Yongfeng
  • 345
  • 1
  • 3
  • 15
  • I just afried that the process `Java(TM) Platform SE binary` will become the isolated process and take too much computer resources :( **WHO CAN HELP ME** – Yongfeng Dec 12 '16 at 12:16
  • Maybe this helps? http://stackoverflow.com/questions/4789837/how-to-terminate-a-python-subprocess-launched-with-shell-true – bert Dec 12 '16 at 12:21
  • @bert Thanks for your help, But when I use `os.killgp(os.getgpid(proc.pid), signal.SIGTERM)` on WINDOWS plarform, It will throw exception that `module os has no attribute of 'killgp'`, maybe I should search more for my problem. – Yongfeng Dec 12 '16 at 13:17
  • @bert FFFFFinally, I have to kill all the children process the `proc` has generated by kill the `proc` on WINDOWS command **taskkill**. Oh, whish all is well. Thank you all the same :) – Yongfeng Dec 13 '16 at 06:12

1 Answers1

1

After having tried some other functions such as subprocess.run, subprocess.call and so on, all are failed to kill the child process generated by proc on WINDOWS. Maybe these functions still only work for LINUX.

So I finally use WINDOWS command taskkill to kill the subprocess (i.e., proc), my final codes are as follows and the Java(TM) Platform SE binary is killed after I kill proc,

proc = subprocess.Popen("mvn test", shell = True)
try:
    proc.wait(60)  # set timeout of proc
except subprocess.TimeoutExpired:
    # kill the proc if it timed out
    subprocess.call(["taskkill", "/F", "/T", "/PID", str(proc.pid)], shell = True)  
    print("subprocess is killed")
Yongfeng
  • 345
  • 1
  • 3
  • 15
  • This or a similar issue has been fixed in maven-surefire-plugin ("maven test") as of version 2.19. See the [bug](https://issues.apache.org/jira/browse/SUREFIRE-524). – Evgeni Sergeev Jul 26 '18 at 02:02
  • Hi, @EvgeniSergeev ,thanks for your suggestion, maybe the latest sure-fire plugin fixed this bug. Besides, I have blamed this bug to python language before, but if we can solve this problem by fixing the plugin, thing becomes easy. – Yongfeng Jul 28 '18 at 02:06