0

I am trying to kill the application in Ubuntu. I have the following code:

import {ChildProcess, exec} from "child_process";

export default class VisiviTTS {
    private static process?: ChildProcess;

    public static speak(text: string): void {
        this.process = exec(`espeak -v czech "${text}"`);
    }

    public static stop() {
        this.process?.kill("SIGINT");
    }
}

On ArchLinux, everything works. On Ubuntu, it does not kill the process. I have tried SIGTERM, SIGKILL, destroying stderr and stdout, emitting exit and kill on it, but without any success.

skuroedov
  • 77
  • 3

1 Answers1

0

When executing something via NodeJS's exec(), it returns the ChildProcess of /bin/sh, where is executed my script.

Ubuntu does not act like ArchLinux, and if I kill the parent process, child processes will be still alive and probably orphaned. Thus my script is still alive.

So I wrote a recursive function, which kills all children of the process.

import {ChildProcess, exec} from "child_process";
import {EOL} from "os";

export default class VisiviTTS {
    private static process?: ChildProcess;

    public static speak(text: string): void {
        this.process = exec(`espeak -v czech "${text}"`);
    }

    public static stop() {
        if(this.process?.kill("SIGINT"))
            this.kill(this.process?.pid);
    }

    static kill(pid?: number) {
        console.log(pid);
        exec(`ps -o pid= --ppid ${pid}`, (err, stdout) => {
            console.log(err);

            stdout.split(EOL).forEach((line: string) => {
                const num = Number(line);
                if (num > 0) {
                    this.kill(num);

                    console.log(`Killing process ${num}`);
                    exec(`kill -9 ${num}`)
                }
            });
        });
    }
}
skuroedov
  • 77
  • 3