Search code examples
node.jselectronroschild-process

How do I run commands on the same and different terminals with child_process?


I want to perform multiple terminal events with one button. But after running the roscore command in one terminal, I must run two more commands in another terminal before that terminal closes (these two commands have to work in the same terminal). How is this possible?

exec('cd ~/catkin_ws', (error, stdout, stderr) => {
            if (error) {
                if (error.signal == 'SIGTERM') {
                    console.log(error);
                } else {
                    console.log(stderr);
                }
            } else {
                console.log(stdout);
                exec('pwd', (error, stdout, stderr) => {
                    if (error) {
                        if (error.signal == 'SIGTERM') {
                            console.log(error);
                        } else {
                            console.log(stderr);
                        }
                    } else {
                        console.log(stdout);
                    }
                });
            }
        });

For example, I am trying to make the pwd output above be / home / famara / catkin_ws. But I get the result of / home / famara.

I have not tried many ways. I have also read this page but could not.


Solution

  • Here you do something like this. In a simpler way.

    cd ~ returns you to your user account's home folder.

    pwd It prints the current directory name with the complete path starting from root (/)

    exec('cd ~/catkin_ws && pwd', (error, stdout, stderr) => {
     if (error) {
      if (error.signal == 'SIGTERM') {
       console.log(error);
      } else {
       console.log(stderr);
      }
     } else {
      console.log(stdout);
     }
    });