This is the 23rd day of my participation in the August More Text Challenge

preface

To exit a running NodeJS program, either Ctrl + C or process.exit() can be used.

Both operations force the process to exit as soon as possible, even if there are still pending asynchronous operations that are not fully completed, including I/O operations to process.stdout and process.stderr.

If the Node.js process needs to be terminated due to an error condition, it is safer to throw an uncaught error and allow the process to terminate accordingly than to call process.exit(), for example:

import process from 'process';

// How to set the exit code correctly and exit the process normally.
if (someConditionNotMet()) {
  printUsageToStdout();
  process.exitCode = 1;
}
Copy the code

In Worker threads, this function stops the current thread but not the current process.

How do I get exitCode for a NodeJS program that accidentally exits? What does each exit code mean? Today we are going to learn.

Get the exit code from NodeJS’s child_process child

The child_process.fork() method is a special case of child_process.spawn() and is specifically used to spawn new NodeJS processes.

const fork = require("child_process").fork;

console.log("main ", process.argv);

const fs = require("fs");

const fd = fs.openSync("./a.log"."a");

const child = fork("./index.js", {
    stdio: ["ipc"."pipe", fd]
});

child.on("error".(error) = > {
    let info = `child process error ${error}`;
    fs.writeSync(fd, info);
    console.log(info);
});

child.on("exit".(code) = > {
    let info = `child process exited with code ${code}`;
    fs.writeSync(fd, info);
    console.log(info);
});

Copy the code

The subroutine executes the parameters

const fork = require('child_process').fork;

console.log('main ',process.argv);

const fs=require('fs');

const fd = fs.openSync('./a.log'.'a');

// Subroutine arguments
let args = [];
args[0] = 'test';

const child = fork('./index.js',args,{
    stdio: ['ipc'.'pipe',fd]
});

child.on('error'.(error) = > {
    let info = `child process error ${error}`;
    fs.writeSync(fd,info);
    console.log(info);
});

child.on('exit'.(code) = > {
    let info = `child process exited with code ${code}`;
    fs.writeSync(fd,info);
    console.log(info);
});
Copy the code

NodeJS exit code

NodeJS usually exits with a zero status code when no more asynchronous operations are pending. Use the following status codes in other cases:

  • 1 Fatal exception not caught: There is an uncaught exception and it is not fielded'uncaughtException'Event handle handling.
  • 2: unused (reserved by Bash for built-in misuse)
  • 3 Internal JavaScript parsing error: Internal JavaScript source code caused parsing errors during NodeJS boot. This is extremely rare and usually only happens during NodeJS development itself.
  • 4 Internal JavaScript evaluation failed: Internal JavaScript source code during NodeJS bootstrap failed to return function values during evaluation. This is extremely rare and usually only happens during NodeJS development itself.
  • 5 A fatal error: Fatal error in V8 that cannot be recovered. It is usually printed with a prefixFATAL ERRORMessage to standard error.
  • 6 Internal exception handle for a non-function: There is an uncaught exception, but the internal fatal exception handle is somehow set to non-function and cannot be called.
  • 7 The internal exception handle failed to run: There is an uncaught exception, and the internal fatal exception handle function itself throws an error when it tries to handle it. For example, if'uncaughtException'domain.on('error')This happens when the handle throws an error.
  • 8: Not used. In previous versions of NodeJS, the exit code 8 sometimes indicated an uncaught exception.
  • 9 Invalid parameter: An unknown option is specified, or an option that requires a value is provided without a value.
  • 10 The internal JavaScript runtime failed: Internal JavaScript source code during NodeJS bootstrap throws an error when calling bootstrap functions. This is extremely rare and usually only happens during NodeJS development itself.
  • 12 Invalid debug parameter: set up--inspectAnd/or--inspect-brkOption, but the selected port number is invalid or unavailable.
  • 13 The unfinished top layer waits: used outside functions in top-level codeawaitBut incomingPromiseNever solved.
  • > 128 Exit signal: If NodeJS receives a fatal signal, for exampleSIGKILLSIGHUP, the exit code will be128Plus the value of the signal code. This is standard POSIX practice because the exit code is defined as a 7-bit integer, and the signal exit setting is high and then contains the value of the signal code. For example, signalsSIGABRTThe value is6, so the expected exit code will be128 + 6134.

conclusion

This is how to get the NodeJS program exit code and the exit code enumeration.

~

Thanks for reading!

~

Learn interesting knowledge, meet interesting friends, shape interesting soul!

Hello everyone, I am the author of “programming Samadhi”, I am king Yi, my public account is “programming Samadhi”, welcome to pay attention, I hope you can give me more advice!

You come, with expectations, I have ink to welcome! You return, no matter gain or loss, only to yu Yun give each other!

Knowledge and skills should be paid equal attention to, internal force and external power should be repaired simultaneously, theory and practice should grasp both hands, both hands should be hard!