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!
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; }
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.
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); });
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 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
.
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!