Home > Web Front-end > JS Tutorial > body text

A brief discussion on how to obtain the program exit code in NodeJS

青灯夜游
Release: 2021-09-29 09:39:15
forward
1755 people have browsed it

How to get the NodeJS program exit code? The following article will introduce to you the method of obtaining the Node.js program exit code and the exit code enumeration. I hope it will be helpful to you!

A brief discussion on how to obtain the program exit code in NodeJS

To exit the running NodeJS program, we can either use Ctrl C or process.exit() to execute exit. [Recommended study: "nodejs Tutorial"]

Both operations will force the process to exit as soon as possible, even if there are still incomplete asynchronous operations pending, including for process I/O operations for .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 rather than calling process.exit(), such as:

import process from 'process';

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

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

So how to get the exitCode for some NodeJS programs that exit unexpectedly? What does each exit code mean? Let’s learn about it 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(), specially 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 after login

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);
});
Copy after login

NodeJS exit code

NodeJS usually exits with 0 status code when there are no more asynchronous operations pending quit. Use the following status code in other cases:

  • 1 Uncaught Fatal Exception: An uncaught exception exists and it is not covered by a domain or 'uncaughtException' Event handler processing.
  • 2: Unused (reserved by Bash for built-in misuse)
  • 3 Internal JavaScript parsing error: Internal JavaScript source code during NodeJS bootstrapping causes parsing errors. This is extremely rare and usually only happens during the development of NodeJS itself.
  • 4 Internal JavaScript evaluation failed: The internal JavaScript source code during NodeJS bootstrapping failed to return a function value when evaluated. This is extremely rare and usually only happens during the development of NodeJS itself.
  • 5 FATAL ERROR: An unrecoverable fatal error exists in V8. Normally a message prefixed with FATAL ERROR will be printed to standard error.
  • 6 Internal exception handler for non-function: There is an uncaught exception, but the internal fatal exception handler is somehow set to a non-function and cannot be called.
  • 7 Internal exception handler runtime failure : An uncaught exception existed, and the internal fatal exception handler function itself threw an error when trying to handle it. This would occur, for example, if the 'uncaughtException' or domain.on('error') handle threw an error.
  • 8: Not used. In previous versions of NodeJS, exit code 8 sometimes indicated an uncaught exception.
  • 9 Invalid parameter: An unknown option was specified, or an option requiring a value was provided without a value.
  • 10 Internal JavaScript runtime failure: The internal JavaScript source code during NodeJS bootstrap throws an error when calling the bootstrap function. This is extremely rare and usually only happens during the development of NodeJS itself.
  • 12 Invalid debug parameter: --inspect and/or --inspect-brk options are set , but the selected port number is invalid or unavailable.
  • 13 Unfinished top-level await: await is used outside a function in top-level code, but the Promise passed in Never resolved.
  • >128 Signal exit: If NodeJS receives a fatal signal, such as SIGKILL or SIGHUP, then Its exit code will be plus the value of the signal code. This is standard POSIX practice, since exit codes are defined as 7-bit integers, and signal exits set the high bit and then contain the value of the signal code. For example, the value of signal SIGABRT is 6, so the expected exit code would be 6 or 134 .

Summary

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

~This article is over, thank you for reading!

For more programming related knowledge, please visit: Programming Video! !

The above is the detailed content of A brief discussion on how to obtain the program exit code in NodeJS. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:juejin.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template