node. JS Notes (ix) Nodejs and Shell

Source: Internet
Author: User

As we all know, Nodejs is a single-process asynchronous operation, but not just a single process, Nodejs also provides multi-process support
Its founders should still pay more attention to this piece, the strongest evidence is that child_process is one of the NODEJS core modules

This module should not be used in most cases, but child_process can do a lot of interesting things.

Shell calls

Recently used more practical functions, in large data processing, the need to be based on the customer click on the page to convert to spark cluster command
Spark was written in Scala, and it didn't have anything to do with Nodejs.
After considering a period of time, decided to use the Shell to solve

Shell Script Basics
The shell is actually very simple, you lose a CD desktop in the console and then enter, this is the simplest shell command,
Writing this line of instructions in the text is the shell script.
For example:
test.sh

#!bin/bashspark-submit test.jar para1 para2......

Commands can be used under the *nix system

sh test.sh

To execute this script with the effect of a direct knock command

spark-submit test.jar para1 para2......

is the same.

the ExecFile method of Nodejs
The prototype is as follows:

child_process.execfile (file, args, options, callback) Description: File {string}  the file name of the program to run args {array}  string parameter list options {object}  cwd {string}  Sub-process current working directory env {object}  environment variable key value pair encoding {string}  Encoding (default =  ' UTF8 ' ) timeout {number}  timeout (default = 0 ) maxbuffer {number}  maximum buffering (default = 200  *1024 ) killsignal {string}  end signal (default =  ' SIGTERM ' ) callback {function}  process end callback with output error Span class= "Hljs-list" >{error}  stdout {buffer}  stderr {buffer}  return: Childprocess object 

The use is also very simple, it is worth noting that there are three points

    1. Timeout
    2. StdOut
    3. StdErr

Using the shell to implement this cross-language invocation is often considered unstable,
Once the Linux EXEC command executes, the original process is replaced with a new process, which loses control of the new process.
For the first time, there was a shell error, for various reasons card death situation
Nodejs provides a better solution, and timeout solves the problem of card death
STDOUT and stderr provide standard output and error output so that the state of the child process can be obtained
Here's how to use it:

var  call_sh = require  ( ' child_process ' ); //para is an array of JSON format, which is obtained by post  function  callsh  Span class= "Hljs-params" > (File,para)  { call_sh.execfile (File,[para.attr1,para.attr2],function   (Error, stdout, stderr)     { console.log ( ' stdout: '  + stdout);    Console.log ( ' stderr: '  + stderr); if  (Error!== null )    {Console.log ( + error); }    });}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

node. JS Notes (ix) Nodejs and Shell

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.