How kill executables started with spawnShell or executeShell when program finish?

Ferhat Kurtulmuş aferust at gmail.com
Tue Oct 27 19:23:22 UTC 2020


On Tuesday, 27 October 2020 at 15:16:33 UTC, Marcone wrote:
> Becouse my program use plink.exe running with spawnShell or 
> executeShell.
> But when my program finish with some crash, or killed with 
> windows task manager by user, Plink still running. How can I 
> stop all process initialized with spawnShell or executeShell 
> when program finish? I another works, how can I make plink.exe 
> only lives when program is running?

IMHO, your d program cannot have direct control over a spawned 
process. However, I suggest a road map for you, although I am not 
sure if it works.

- I don't know if d has something like C's expect [1] library, 
but you will need a similar thing. Basically, It spawns processes 
and handles their stdout. Maybe you can just wrap libexpect in D.

- Assuming you have "expect" running in D, you can spawn 
"tasklist" [2] and somehow filter out (I recall that it can be 
done with expect using a struct like a regex on stdout) its 
stdout to determine the PID number [2] of the process that you 
want to kill at the end of the program.

- then, in your main:
void main(){
...

     scope(exit){ // maybe you should also be aware of 
scope(success) and scope(failure)
         killPlink(); // spawn another process: "Taskkill /PID 
26356 /F"
     }
...
}
1: 
http://npg.dl.ac.uk/MIDAS/manual/ActiveTcl8.4.9.0-html/expect/libexpect.3.html
2: 
https://tweaks.com/windows/39559/kill-processes-from-command-prompt/


More information about the Digitalmars-d-learn mailing list