digitalmars.D.learn - How kill executables started with spawnShell or executeShell when
- Marcone (7/7) Oct 27 2020 Becouse my program use plink.exe running with spawnShell or
- Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= (26/33) Oct 27 2020 IMHO, your d program cannot have direct control over a spawned
- Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= (5/11) Oct 27 2020 Ohh, spawnShell returns a pid, so you don't need to use expect
- Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= (4/17) Oct 27 2020 There is also this:
- Jack (8/15) Oct 27 2020 if you want to close plink.exe when the application that started
- Dukc (10/17) Oct 27 2020 This is a bit heavyweight, but should be doable: have your
- Jack (4/15) Oct 27 2020 In case of windows, the OS can take care of this with the JOB
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
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
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: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[...]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. [...]
Oct 27 2020
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:There is also this: https://dlang.org/library/std/process/kill.htmlOn Tuesday, 27 October 2020 at 15:16:33 UTC, Marcone wrote: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[...]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. [...]
Oct 27 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?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
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
On Tuesday, 27 October 2020 at 22:14:53 UTC, Dukc wrote:On Tuesday, 27 October 2020 at 15:16:33 UTC, Marcone wrote: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[...]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