头图

Preface

To exit the running NodeJS program, we can either use Ctrl + C or process.exit() to exit.

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

If the Node.js process needs to be terminated due to an error condition, throwing an uncaught error and allowing the process to terminate process.exit() is safer than calling 0612baf0a6cd60, for example:

import process from 'process';

// 如何正确设置退出码,同时让进程正常退出。
if (someConditionNotMet()) {
  printUsageToStdout();
  process.exitCode = 1;
}

In the Worker thread, this function stops the current thread instead of the current process.

So for some accidental launch situations, how to get exitCode? What does each exit code represent? Let’s learn today.

Get the exit code through the child_process child process of NodeJS

The child_process.fork() method is a special case of child_process.spawn(), 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);
});

Subroutine execution parameters

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

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

const fs=require('fs');

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

// 子程序参数
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);
});

NodeJS exit code

When there are no more asynchronous operations pending, NodeJS usually exits with a status code of 0 Use the following status codes in other situations:

  • 1 fatal exception : There is an uncaught exception, and it is not 'uncaughtException' event handler.
  • 2 : Not used (reserved by Bash for built-in misuse)
  • 3 internal JavaScript parsing error : The internal JavaScript source code during the NodeJS boot process caused a parsing error. This is extremely rare and usually only happens during the development of NodeJS itself.
  • 4 internal JavaScript evaluation failed : The internal JavaScript source code in the NodeJS boot process failed to return the function value during evaluation. This is extremely rare and usually only happens during the development of NodeJS itself.
  • 5 Fatal error : There is an unrecoverable fatal error in V8. Usually the message with the prefix FATAL ERROR will be printed to standard error.
  • 6 Non-function internal exception handle : There is an uncaught exception, but the internal fatal exception handle is somehow set to a non-function and cannot be called.
  • 7 internal exception handle failed runtime 1612baf0a6cf7f: There is an uncaught exception, and the internal fatal exception handle function itself throws an error when trying to handle it. For example, 'uncaughtException' or domain.on('error') handle throws an error.
  • 8 : Not used. In previous versions of NodeJS, 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 Internal JavaScript runtime failed : The internal JavaScript source code in the NodeJS boot process throws an error when calling the boot function. This is extremely rare and usually only happens during the development of NodeJS itself.
  • 12 Invalid debugging parameters : The --inspect and/or --inspect-brk options are set, but the selected port number is invalid or unavailable.
  • 13 unfinished top floor waiting : outside the top-level function in your code uses await , but incoming Promise never resolved.
  • >128 signal exit : If NodeJS receives a fatal signal, such as SIGKILL or SIGHUP , its exit code will be 128 plus the signal code. This is standard POSIX practice because the exit code is defined as a 7-bit integer, and the signal exit is set to the high bit, and then contains the value of the signal code. For example, the value of the SIGABRT 6 , so the expected exit code will be 128 + 6 or 134 .

Summarize

The above is the method to get the exit code of NodeJS program and the exit code enumeration.

~

~ End of this article, thanks for reading!

~

Learn interesting knowledge, meet interesting friends, and create interesting souls!

Hello everyone, I am , the author Programming Samadhi Hermit King , my public is "1612baf0a6d1cc Programming Samadhi ", I hope you can pay more attention!

Come, with expectations, I have Moxiang to greet you! You return, no matter the gains or losses, only with the lingering rhyme as a gift!

Pay equal attention to knowledge and skills, both internal and external skills, both theory and practice must be grasped, and both hands must be hard!


编程三昧
54 声望10 粉丝

学习有趣的知识,交识有趣的朋友,造就有趣的灵魂!