13

I am running my node server in centos6.X, and forking a child process.

var cp = require('child_process');
child = cp.fork(__dirname+'/worker');
child.on('exit', function (code, signal) {
            console.log('Child exited with code = ' +code+'  signal = '+signal);
 });

but after few seconds I get this error

Child exited with code = null signal = SIGKILL

I am now clueless how to get rid of this, there is no more trace, as to what is happening.

more clarification

I have put console.logs in worker and they are getting printed. the worker is now waiting for message from main server. but exits after waiting for few seconds. I also run the worker js independently with node command and there it does not exit.

The same code works fine on my local OSX laptop. but on cloud centos6.X I am facing this issue.

1
  • I'm not very familiar with the .fork nodejs API but I think it takes in a module, which I think would typically be a .js file that has something like module.export? How does your worker file look like? Or maybe you may have missed the .js bit?
    – Samuel Toh
    Aug 26, 2016 at 6:19

2 Answers 2

32

There are two ways a child process can exit: voluntarily, in which case an exit code will be returned, or involuntarily, through a signal.

From the arguments of the exit handler, the first reflects the exit code, and the second the signal. One of these will contain a value, the other one will be null. As explained in the documentation:

If the process exited, code is the final exit code of the process, otherwise null. If the process terminated due to receipt of a signal, signal is the string name of the signal, otherwise null. One of the two will always be non-null.

If your child process receives a SIGKILL, and you're not the one that (explicitly) sent that signal, there's a good chance that the child process got killed by the Linux OOM killer, which kills processes in case the system is strapped for memory ("OOM" means "Out Of Memory").

So make sure that your system has enough RAM, enough swap, and/or that your process doesn't take up too much memory (if possible).

1
  • I just tried to add swap memory and it worked. thanks.
    – enRaiser
    Aug 26, 2016 at 15:46
5

In my case, the process run by exec was printing too much output and the maxBuffer was reached (see maxBuffer option of exec), which caused NodeJS to detach from the child process with a null exit code, while the child process kept running in the background until it finished successfully.

If this is your case as well, the solution is to simply increase the maxBuffer option, which is 1MB by default (as of writing this).

Thanks to Sathish for pointing this out.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.