0

actually I run my exec task in an async function. After execution a software starts and runs in a loop.

After this execution, my node.js app stops going on with the code.

How can I start running another app out of node.js and ignore the result and go on with the code?

import util = require('util');

const exec = util.promisify(require('child_process').exec);
export class ExecClass{
    public static async run(...parameters: (number | string)[]) {
        const app=parameters[0];
        parameters=parameters.slice(1);
        const cmd = app+ " " + parameters.join(" ");
        const { stdout, stderr } = await exec(cmd);
    }
}

EDIT:

After my calling code I just make some edits in my database.

my calling code:

import {ExecClass} from "./exec-class";
async function run() {
    await ExecClass.run(cmd, data.id, data.startDate, data.endDate, data.idx);
}
user3241084
  • 87
  • 1
  • 1
  • 11
  • `child_process.exec()` does not block. So, your nodejs program should be able to do other things while that is running. Can you show the calling code and what else you're expecting to do after you call this? – jfriend00 Mar 04 '21 at 01:08
  • I just added my calling code – user3241084 Mar 04 '21 at 09:48
  • That doesn't show anything useful. What else are you trying to do. Your question says "my node.js app stops going on with the code". So, what else do you want it to execute and where is that code? Are you seeing any errors? Or does it just wait until the exec is done before proceeding? – jfriend00 Mar 04 '21 at 22:06
  • It just waits until exec is done, after that it just goes on with my code. The following code is just some calculation – user3241084 Mar 06 '21 at 15:40
  • Then, just remove the `await` in front of `ExecClass.run()` if you don't want your code to wait for it. You should put a `.catch()` on it though so you don't have unhandled rejections. – jfriend00 Mar 06 '21 at 23:35

1 Answers1

0

I do not have a moment to test this but it seams like all you need to do is remove the await keyword from the exec() call. You'll no longer need the async modifier on the run function either.

import util = require('util');

const exec = util.promisify(require('child_process').exec);
export class ExecClass{
    public static run(...parameters: (number | string)[]) {
        const app=parameters[0];
        parameters=parameters.slice(1);
        const cmd = app+ " " + parameters.join(" ");
        const { stdout, stderr } = exec(cmd);
    }
}

Now you'll no longer be able to treat exec like a traditional synchronous function so you'll have to change that back to callback format:

import util = require('util');

const exec = util.promisify(require('child_process').exec);
export class ExecClass{
    public static run(...parameters: (number | string)[]) {
        const app=parameters[0];
        parameters=parameters.slice(1);
        const cmd = app+ " " + parameters.join(" ");
        exec(cmd, ({ stdout, stderr }) => {
           // do something with stdout or stderr here
        });
    }
}

but you'll most like want to keep your promise oriented flow so we can wrap your whole operation in a promise:

import util = require('util');

const exec = util.promisify(require('child_process').exec);
export class ExecClass{
    public static run(...parameters: (number | string)[]) {
        return new Promise( (resolve, reject) => {
            const app=parameters[0];
            parameters=parameters.slice(1);
            const cmd = app+ " " + parameters.join(" ");
            exec(cmd, ({ stdout, stderr }) => {
               resolve({stdout, stderr});
            });
        })
    }
}

Now you can wait for the promise to resolve in your parent calling code while the calling process continues to execute operations. Further, you can handle the promise on your calling code.

Ty Savercool
  • 1,132
  • 5
  • 10
  • thanks for this answer, but my parent calling code still waits until ExecClass run finished – user3241084 Mar 04 '21 at 00:12
  • is there maybe another solution, to start a software with args? I don't need to log stdout, stdin and error – user3241084 Mar 04 '21 at 00:21
  • All three of your code versions are wrong. The first attempts to do `const { stdout, stderr } = exec(cmd);`, but `exec()` here returns a promise so that will fail. In your 2nd and 3rd code blocks `exec()` in this code is a promisified version of `child_process.exec` that does NOT accept a callback so that callback won't do anything. I get that you're just trying to remove the `await`, but that is not necessary. If the calling code just doesn't `await` the promise returned from this function, it can do anything else it wants. – jfriend00 Mar 04 '21 at 01:09
  • Just came back to correct that mistake, sorry I shouldn't be doing drive by answers. – Ty Savercool Mar 04 '21 at 12:24