How to wait for a shell process to finish on ctrl+c before exiting?

aliak something at something.com
Sun Nov 24 15:44:00 UTC 2019


I'm writing some command line tooling stuff, and one of the 
command spins up a docker compose file (which in short, spins up 
some services and aggregates the output of each service to 
stdout).

When a user presses ctrl+c, i would like to pass on the ctrl+c to 
the spawned process and wait till it handles ctrl+c and then let 
go of the current process.

So far I have this:

int spawnedPid;

extern(C) int kill(int pid, int sig) nothrow @nogc @system;

extern(C) void interruptHandler(int sig) nothrow @nogc @system {
     kill(spawnedPid, sig);
}

int spawnProcessAndWait(string[] cmd) {
     auto pid = spawnProcess(cmd, stdin, stdout, stderr);
     spawnedPid = pid.processID;
     signal(SIGINT, &interruptHandler);
     int result = wait(pid);
     return wait(pid);
}

It doesn't work. I think the call to kill doesn't wait? Is there 
a way to make it wait?

I can't call kill(Pid) or wait(Pid) inside the interrupt handler 
because those are not @nogc [0].

[0]: 
https://forum.dlang.org/thread/mtikzznfaahiltguvybw@forum.dlang.org


More information about the Digitalmars-d-learn mailing list