What is the best way to retrieve a PID from a Process that was spawned using the TS Exec Function

When working on a Windows system with TypeScript

I face an issue with a function that launches another application via the command line. I am able to capture the Pid spawned by this Exec Function, but it turns out to be the Pid of the CMD used to initiate the application, not the application itself.

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

This only provides the pid of the CMD process, not the actual application.

I need to store the correct pid so I can use it later to terminate the process using ps.kill.

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

Any suggestions for resolving this issue?

Answer №1

Experimenting with different approaches, I discovered a successful method involved utilizing the spawn function to directly run the function rather than using CMD. This allowed the returned PID to be associated with the correct process.

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What are the best practices for testing a class function that executes a mongoose query?

Within my class, there are multiple class functions, with one specifically executing a mongoose query. The structure is similar to: export class ExampleService { constructor( @InjectModel(Example.name) private exampleModel: Model<Example>, ...

Preserve final variable state - Angular

My function looks like this: flag: boolean = false; some_function(){ var foo = some_num_value; var bar = foo; // Storing value in a separate variable if(this.flag){ v ...

Creating a function that can return two separate data types depending on whether optional input is provided

Take a look at this function: function useCustomElement<T extends HTMLElement>(ref?: React.RefObject<T>) { const elementRef = ref ? ref : useRef(null) const [value, setValue] = useState(false) // perform some operations... return ref ...

Encountered a problem while attempting to post in Angular, receiving an error message stating "net::ERR

I recently started learning Nodejs. I've created an API on a local server using Mysql and I'm working on the frontend with Angular, while using Nodejs and Express as the backend. However, I'm facing an issue where my Angular app cannot conne ...

Obtaining the count of a specific column in Angular 6 by grouping objects based on the same value in an array

In TypeScript, I have an array of objects and I am using a foreach loop. The array contains 2 columns with a progress value of 100, while the rest have values less than 100. My goal is to calculate the count of columns with a progress value of 100, which ...

Dissimilarities in behavior between Angular 2 AOT errors

While working on my angular 2 project with angular-cli, I am facing an issue. Locally, when I build it for production using ng build --prod --aot, there are no problems. However, when the project is built on the server, I encounter the following errors: . ...

Discover more efficient methods for utilizing generics in hierarchical objects within typescript

How can I optimize the structure of an object that contains nested objects in Typescript to minimize type repetitions? type itemType = { [key: string]: { [key: string]: { [key: string]: { [key: string]: string } }; }; }; ...

When a list item is clicked, the 'active' class is added to it and removed from its sibling elements in Angular 8

By default, the first list item will be active. When you click on any list item, it should add a class of 'active' and remove the class from its siblings. public items = [{ value: 'All', }, { value: 'Photos', ...

How can I convert a MongoDB document into a DTO in NestJS?

I'm currently working with a data layer that interacts with a MongoDB database. My goal is to only handle MongoDB documents in this layer without exposing the implementation details to my services. My current approach involves the following code: // ...

Extract a parameter value from a URL included in a GET request. Error message: "Property cannot be read due to TypeError."

Trying to extract data from the URL route parameters in a GET request has become quite a challenge. It seemed to work perfectly fine before, without any changes. Now, nothing seems to be working as expected. I have scattered console.log statements all ove ...

Utilizing the compilerOptions: paths setting does not yield desired results when working with Node

Similar to this related question, but with unique aspects when utilizing node: $ tree . . ├── src │ ├── main.ts │ └── utils │ └── myUtils.ts └── tsconfig.json I am attem ...

Having Trouble with Typescript Modules? Module Not Found Error Arising Due to Source Location Mismatch?

I have recently developed and released a Typescript package, serving as an SDK for my API. This was a new endeavor for me, and I heavily relied on third-party tools to assist in this process. However, upon installation from NPM, the package does not functi ...

Retrieve the name of the current item at the end of the list

Is there a way to retrieve the last item on the list marked as success? children: [ { case: "no-success", name: "bruno", endOffset: 5 }, { case: "no-success", name: "pippo", endOffset ...

What could be causing the parameter "id" to be undefined in Nest JS

Exploring the layout of the project directory, I am currently utilizing yarn berry for the monorepo setup with two distinct packages. The first package houses all the applications, while the second one contains shared libraries and modules. `- apps bff ...

Ways to obtain a tab and designate it as the default in angular when using angular material Tabs

I am facing an issue with accessing tabs within a nested component. The parent component contains the tab feature and to reach the tabs inside the child component, I am using the following code: document.querySelectorAll('.mat-tab-group'); The a ...

Create a TypeScript function that can be called and has an extended prototype definition

I am seeking to create a callable function foo() (without using the new operator) that will also include a property foo.bar(). The JavaScript implementation would be as follows: function foo() { // ... } foo.prototype.bar = function bar() { // .. ...

The information retrieved from the API is not appearing as expected within the Angular 9 ABP framework

I am facing an issue with populating data in my select control, which is located in the header child component. The data comes from an API, but for some reason, it is not displaying correctly. https://i.stack.imgur.com/6JMzn.png. ngOnInit() { thi ...

Revise the observable to trigger a NgbModal from a service

I have a situation in my Angular 11 service where I am using a Ngbmodal component to subscribe when it is closed. Below is the code snippet: showMessage(messageData: MessageDataDTO): Observable<MessageResult> { return new Observable((result) =&g ...

Learn how to successfully import a webp image into a React TypeScript project

I have looked everywhere for the answer, but I can't seem to find it When trying to import a *.webp image in typescript, you need to create a declaration file, like declaration.d.ts The declaration file should contain something similar to the foll ...

Packaging a NodeJS project in Visual Studio - A step-by-step guide to creating and setting up an N

In my VS2013 solution, I have a combination of NodeJS (using TypeScript) and C# class library projects connected by EdgeJS. Among the NodeJS projects, one serves as a library for a RabbitMQ bus implementation, while two are applications meant to be hosted ...