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

Steven Schveighoffer schveiguy at gmail.com
Sun Nov 24 17:04:49 UTC 2019


On 11/24/19 10:44 AM, aliak wrote:
> 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?

Hm.. are you sure that ctrl-c isn't also sending the signal to your 
child process? I thought it did.

-Steve


More information about the Digitalmars-d-learn mailing list