Home > Net >  Arguments for Node child_process "spawn()"
Arguments for Node child_process "spawn()"

Time:12-31

Using child_process (spawn) to open a new BASH terminal and execute the commands in each file.

I want to add all of my .sh files, that I'm going to spawn, into a folder to clean up my project directory.

PROBLEM: I can't figure out how to change the directory the scripts run from, and the docs are a little to heavy for me at this point.

SIMPLE TEST EXAMPLE, WITH MY FIRST TWO FILES FOR CONNECTING (each .sh file would be in the project folder):



    const { spawn } = require('node:child_process');

    const bat = spawn('cmd.exe', ['/c', 'connect.sh']);

/*
    <connect.sh> // 1st .sh file
    #!/usr/bin/env bash  //to make it bash
    HTTP_PORT=3002 P2P_PORT=5002 PEERS=ws://localhost:5001 npm run dev

    <connect1.sh> // 2nd .sh file
    #!/usr/bin/env bash   //to make it bash
    HTTP_PORT=3003 P2P_PORT=5003 PEERS=ws://localhost:5002,ws://localhost:5001 npm run dev
*/

I have 9 of these files to connect up to 10 peers. And I would like to put them in a folder to simplify my project structure.

This is my actual API call below.... // Uses length to determine which file to run



     app.post("/peers/connect", async function (req, res) {
      const peerInfo = await peers.info();
      // no peers yet
      if (typeof peerInfo === "string") {
        let bat = spawn("cmd.exe", ["/c", "connect.sh"]);
        res.json("A new terminal has opened! You are now connected!");
      } else { 
        // peers exist
        let length = peerInfo.peers;
        // console.log(length);
        let bat = spawn("cmd.exe", ["/c", `connect${length}.sh`]);
        res.json("A new terminal has opened! You are now connected!");
      }
    });

My file structure here...you can see why I want a folder for these!

My file structure

RECAP: Help my put all of these files into a folder (shellScripts) and have the code still work :) Thanks! (just realized we might have to cd back into project folder before "npm run dev" in each file?)

CodePudding user response:

You are using the cmd.exe utility to run a .sh file, but that wont work. You have to install a bash interpreter on your windows device or install WSL. (If necessary add bash.exe to the windows path) Then change your code to this:

const { spawn } = require('node:child_process');
const bat = spawn('bash.exe',['connect.sh']);

I hope this answer helped

For running multiple files:

const { spawn } = require('node:child_process');
const fs = require("node:fs")
const dir = "" // Replace this with the location of the directory containing connect shellscripts

let entrys = fs.readdirSync(dir)
entrys = entrys.filter(v => v.startsWith("connect"))

for (let ent of entrys) {
  const bat = spawn('bash.exe',[ent]);
  // your code here
}

CodePudding user response:

Figured out the answer on me own. Thanks to everyone that tried to help :) And to those saying my above code doesn't work, it works perfectly fine.

I've provided a picture to clarify. 1st is what the code below produces. 2nd is manually pasting it into GIT BASH.

// test.js in project structure pic above
var exec = require('child_process').exec;
var path = require('path')

var parentDir = path.resolve(process.cwd(), 'shellScripts');
exec('my.sh', {cwd: parentDir}, function (error, stdout, stderr) {
  // if you also want to change current process working directory:
  process.chdir(parentDir);
});

This is what the code produces. Pic 1. Code output

And this is opening a GIT BASH in project folder and pasting the command in

Pic 2. Manual GIT BASH

  • Related