In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces NodeJS how to obtain the program exit code, the article introduces in great detail, has a certain reference value, interested friends must read it!
To exit a running NodeJS program, we can exit either through Ctrl + C or through process.exit ().
Both operations force the process to exit as soon as possible, even if there are still incomplete asynchronous operations pending, including the IWeiO operation on 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 (), such as:
Import process from 'process';// how to set the exit code correctly while allowing the process to exit normally. If (someConditionNotMet ()) {printUsageToStdout (); process.exitCode = 1;}
In Worker threads, this function stops the current thread instead of the current process.
So for some NodeJS programs that quit unexpectedly, how to get exitCode? What does each exit code represent? Let's study today.
Obtain the exit code through the child_process child process of NodeJS
The child_process.fork () method is a special case of child_process.spawn () and is specifically used to derive 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. Logarithmic grammar); / / subroutine parameter 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 no more asynchronous operations are suspended, NodeJS usually exits with a 0 status code. Use the following status code in other cases:
1 uncaught fatal exception: there is an uncaught exception and it is not handled by the domain or 'uncaughtException' event handle.
2: not used (reserved by Bash for built-in misuse)
3 Internal JavaScript parsing error: internal JavaScript source code during NodeJS boot process causes 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 during NodeJS boot failed to return function values 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. Messages with the prefix FATAL ERROR are usually 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 runtime failure: there is an uncaught exception, and the internal fatal exception handle function itself throws an error when trying to handle it. For example, this happens if the 'uncaughtException' or domain.on (' error') handle throws an error.
8: not in use. In previous versions of NodeJS, the exit code 8 sometimes represented an uncaught exception.
9 invalid parameter: an unknown option is specified, or an option that requires a value but no value is provided.
10 Internal JavaScript runtime failure: the internal JavaScript source code during NodeJS boot 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-level wait: await is used outside the function in the top-level code, but the Promise passed in is never resolved.
> 128signal exit: if NodeJS receives a fatal signal, such as SIGKILL or SIGHUP, its exit code will be 128 plus 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 sets the high bit, and then contains the value of the signal code. For example, the value of the signal SIGABRT is 6, so the expected exit code will be 128 + 6 or 134.
The above is all the contents of the article "how to get the exit Code for NodeJS". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.