Search code examples
typescriptwindowspid

How to get pid from Processed spawned by TS Exec Function?


On Windows and using TypeScript

I have a function which executes another application by command line, I then capture the Pid spawned by this Exec Function, only problem is, it's the Pid of the CMD used to spawn the application, and not the application itself.

pid = exec('cd location && python appName.py', (err, data, getter) =>{
   if (err){
      console.log(err)
   }
}).pid;

This will return the pid of the CMD used to execute the command.

I want to capture and save the pid so I can later use it to kill the process again by using ps.kill.

ps.kill(pid, (err) =>{});

Any ideas ?


Solution

  • After trying a few things, a Method I got to work was by using the spawn function to directly execute the function, instead of going through CMD , so the pid returned in that case will be for the correction process.

    const spawnProcess = spawn(pythonExecutable,[appPath]);
    const processID = spawnProcess.pid;