1

I have a batch script which gets run on a Windows-based Jenkins node (meaning headless, no [GUI] windows involved). The batch script already has its standard input incapacitated by way of < NUL, so as to avoid the "Terminate batch job (Y/N)" prompt.

The batch script performs some preparations and ultimately calls a Bash script running either in Cygwin or MSYS2 (depends on the node). From there many new processes are created, other Bash scripts invoked, other batch scripts invoked etc.

Question: Is there a way, from the command line -- namely cmd.exe --, to create a job object to "herd" the processes created by the initial batch script and processes created by those in turn ...?

The goal here is to tear down all processes in the job forcibly -- much like would be done if processes in a job exceed resource quotas -- as soon as the initial script terminates (whichever way!).

Alternatively: If cmd.exe is too limited, what other utilities onboard a "standard" Windows 10/11 installation can facilitate this?

PS: Process Explorer from Microsoft/Sysinternals has a feature called "Kill Process Tree". This is approximately what I'd need. Alas, the "process tree" is not a reliable mechanism either way. It relies on matching parent PIDs inside the children and there is simply no guarantee that parents outlive their children and semantics differ quite a bit from Unix, too.

0xC0000022L
  • 7,544
  • 10
  • 54
  • 94

2 Answers2

1

A simple method is to create a user account that is dedicated to that task.

The initial batch script can then be launched RunAs this user account, which means that all subsequent child tasks will also be running under the same user account.

Killing all the tasks can then be done via :

taskkill /f /fi "USERNAME eq user-account-name"

This command may need to be run with administrator permissions.

harrymc
  • 498,455
0

From a CMD, you can use the command title to rename the title of that window.

For example, you can set the title as follows: title kill me later

You would want to do this for every process that is being spawned. You can create a batch file that first changes the title and then starts another program if you have to.

Later in your batch script, you can now query these windows using:

tasklist /FI "WINDOWTITLE eq kill me later" 

and if you like what you see, you can terminate it using:

taskkill /FI "WINDOWTITLE eq kill me later" /F

which will close every window that has the title kill me later

the /f switch will forcefully close the window. Ommitting this parameter will try to close it, but may give a prompt whether or not you want to close the window.

Do note, cmd cannot close itself this way. It will simply not find the window.

enter image description here

Also, if you use Windows Terminal, and you spawn a CMD instance inside, using this method will not just close that CMD instance, but it will close the entire Windows terminal instance.

LPChip
  • 66,193