How to wait and kill a Powershell timeout process

Using Powershell 2.0 on the Windows 7 Desktop

I want to create a process to run the ant command, and then wait for this process to complete in a few minutes. If time and process still work, I want to kill him.

I wrote the code as below:

$p = Start-Process "cmd.exe" '/c ant execute' -PassThru -RedirectStandardOutput $log_file_path Wait-Process -id $p.ID -timeout 100 if(!$p.hasExited) { echo "kill the process" $p.Kill() #Stop-Process $p.ID } Start-Sleep -s 30 # continue to do other things 

Although the process has not been killed, it still works even after executing the Start-Sleep instruction.

I also tried using the Stop-Process command, as the commented line indicates that no luck, the process is still running.

Perhaps I missed something important, give me the key.

EDIT:

It turns out that in the cmd window, the child processes are still running, kill only the cmd process.

Finally, I did the work with the following code:

 if(!$p.hasExited) { echo "kill the process" taskkill /T /F /PID $p.ID #$p.Kill() #Stop-Process $p.ID } 
+6
source share
2 answers

If you want to kill the entire process tree, you also need to find and stop child processes.

Here is a good article that explains this and shows you how to do it:

http://powershell.com/cs/blogs/tobias/archive/2012/05/09/managing-child-processes.aspx

+2
source

I clicked on a small github project that can be used to start the process and installation time for it and for its child processes: https://github.com/burlachenkok/process_tool

These are sources with a Makefile, and they are not a powershell script. But hope this can be useful for you.

0
source

Source: https://habr.com/ru/post/956525/


All Articles