Home > database >  child_process.execSync(start "" "example.exe") freezing/locking console with exe
child_process.execSync(start "" "example.exe") freezing/locking console with exe

Time:07-31

When I try to execute an executable that have an option to have parameters. It will freeze the nodejs output and input until the executable is closed. Executables that do not need params will just run, and the nodejs console will not freeze/lock input nor output.

Example with param: test.exe -thisisaparam. Example without Params: test.exe.

Here is my code below. (Its a cli)

const cp = require('child_process');
let start = async function (start) {
    let command = `start "" ${start}`;
    cp.execSync(command);
    console.log("Returning to menu in 10 seconds...")
    setTimeout(() => {
        run()
    }, 10000);
};

Here is how I call the function.

async function startTest() {
      await start("C:\users\user\downloads\test_param.exe")
}

Thanks, Kiefer.

CodePudding user response:

Any command you run using execSync will be synchronous meaning it will wait for the command to exit and then returns the output.

If you don't need the output of the command and want to just start and detach you should use spawn with unref().

example:

const scriptPath = "C:\users\user\downloads\test_param.exe"
cp.spawn('start', [scriptPath], {detached: true, stdio: 'ignore'}).unref() 
  • Related