Search code examples
javascriptnode.jsshellterminalchild-process

Is possible spawn terminal using NodeJS and trigger closeHandler if that terminal has been Closed (with press close button) and with CTRL+C


Hello!

My OS currently in Linux, my goal is to .spawn() another terminal, and watch the parent process output with that terminal using command tail -f app.log, it's run very well using this...

Code

const xec = spawn('gnome-terminal', ['-x', 'bash', '-c', '"tail -f app.log; exec bash"'], {
    shell: true
});

but I can't trigger that .spawn() if the terminal has been Closed and with CTRL+C, even using this...

Code

// Trigger when terminal is show
xec.stdout.on('end', () => {
    console.log('stdout: End');
});

// Not response
xec.stdout.on('exit', () => {
    console.log('stdout: Exit');
});

// Trigger when terminal is show
xec.stdout.on('close', () => {
    console.log('stdout: Close');
});

// Not response
xec.on('close', () => {
    console.log('xec: Close');
});

// Trigger when terminal is show
xec.on('exit', () => {
    console.log('xec: Exit');
});

Solution

  • I can trigger the close button and also with CTRL+C too with this...

    Code

    const command = 'gnome-terminal --disable-factory -- /bin/bash -i -c "tail -f app.log; exec bash"'
    const xec = exec(command, (err, out, derr) => {
            console.log(`Error: ${err}`);
            console.log(`Out: ${out}`);
            console.log(`derr: ${derr}`);
    });
    

    I am just adding --disable-factory. Also --disable-factory didn't support with a newer version but so far I am using v3.28.1 it's still works fine. See this issue also this blog