www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How kill executables started with spawnShell or executeShell when

reply Marcone <marcone email.com> writes:
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?
Oct 27 2020
next sibling parent reply Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= <aferust gmail.com> writes:
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/
Oct 27 2020
parent reply Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= <aferust gmail.com> writes:
On Tuesday, 27 October 2020 at 19:23:22 UTC, Ferhat Kurtulmuş 
wrote:
 On Tuesday, 27 October 2020 at 15:16:33 UTC, Marcone wrote:
 [...]
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. [...]
Ohh, spawnShell returns a pid, so you don't need to use expect actually [1] 1: https://dlang.org/library/std/process/pid.process_id.html
Oct 27 2020
parent Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= <aferust gmail.com> writes:
On Tuesday, 27 October 2020 at 19:30:06 UTC, Ferhat Kurtulmuş 
wrote:
 On Tuesday, 27 October 2020 at 19:23:22 UTC, Ferhat Kurtulmuş 
 wrote:
 On Tuesday, 27 October 2020 at 15:16:33 UTC, Marcone wrote:
 [...]
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. [...]
Ohh, spawnShell returns a pid, so you don't need to use expect actually [1] 1: https://dlang.org/library/std/process/pid.process_id.html
There is also this: https://dlang.org/library/std/process/kill.html
Oct 27 2020
prev sibling next sibling parent Jack <jckj33 gmail.com> writes:
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?
if you want to close plink.exe when the application that started it closes by user's request, crash or forceully by the task manager, WIN32API's JOB functions does that job. I've used that easily. Here's the code[1]. Hope it's useful. [1]: https://stackoverflow.com/questions/6266820/working-example-of-createjobobject-setinformationjobobject-pinvoke-in-net/9164742#9164742
Oct 27 2020
prev sibling parent reply Dukc <ajieskola gmail.com> writes:
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?
This is a bit heavyweight, but should be doable: have your primary process to start a watchdog process for itself. The watchdog continuosly sends messages to the primary process. If the message gets blocked or the watchdog receives no answer, it assumes the primary process has stopped working and thus terminates first plink.exe first and then itself. In fact, while you're on it you can make the watchdog to terminate the primary process too, so the user won't have to kill the program manually in case of infinite loop.
Oct 27 2020
parent Jack <jckj33 gmail.com> writes:
On Tuesday, 27 October 2020 at 22:14:53 UTC, Dukc wrote:
 On Tuesday, 27 October 2020 at 15:16:33 UTC, Marcone wrote:
 [...]
This is a bit heavyweight, but should be doable: have your primary process to start a watchdog process for itself. The watchdog continuosly sends messages to the primary process. If the message gets blocked or the watchdog receives no answer, it assumes the primary process has stopped working and thus terminates first plink.exe first and then itself. In fact, while you're on it you can make the watchdog to terminate the primary process too, so the user won't have to kill the program manually in case of infinite loop.
In case of windows, the OS can take care of this with the JOB functions family: https://docs.microsoft.com/en-us/windows/win32/api/jobapi2/nf-jobapi2-assignprocesstojobobject
Oct 27 2020