Nodejs call scripts (python/shell) and system commands
The node. js subprocess provides important interfaces for interaction with the system. Its main APIs include: standard input, standard output, and standard error output interfaces child. stdin gets the standard input child. stdout gets the standard output child. stderr gets the standard error output and obtains the PID of the sub-process: child. pid provides the method to generate sub-processes: child_process.spawn (cmd, args = [], [options]) provides the method to directly execute system commands: child_process.exec (cmd, [options], callback) provide the method to call the script file: child_process.execFile (file, [args], [options], [callback]) provides the method to kill the process: child. kill (signal = 'signature') is interesting to use instances ~~ 1. Use a sub-process to call system commands (obtain system memory usage) to create a nodejs file named pai_spawn.js. The Code is as follows: var spawn = require ('child _ Process '). spawn; free = spawn ('free', ['-m']); // capture the standard output and print it to the console free. stdout. on ('data', function (data) {console. log ('standard output: \ n' + data) ;}); // capture the standard error output and print it to the console free. stderr. on ('data', function (data) {console. log ('standard error output: \ n' + data) ;}); // register the sub-process to close the event free. on ('exit ', function (code, sign Al) {console. log ('child process eixt, exit: '+ code) ;}); the result of running this script is exactly the same as that of running the 'free-m' command directly: 2nd pipeline executes the system command (child_process.exe c (), which I still use very often and feels a little more powerful than above. For example, I like the weather very much. Now I want to curl the weather interface to return json-format data. Maybe I want to perform some operations on it. I will print it out here without any operation. Create a nodejs file named cmd_exec.js: var exec = require('child_process'cmd.exe c; var response STR = 'curl response (Response STR, function (err, stdout, stderr) {if (err) {console. log ('get weather api error: '+ stderr);} else {/* the content of this stdout is the one I got from the above curl: {"weatherinfo ": {"city": "Beijing", "cityid": "101010100", "temp": "3", "WD": "northwest wind", "WS ": "Level 3", "SD": "23%", "WSE": "3", "time": "", "isRada R ":" 1 "," Radar ":" JC_RADAR_AZ9010_JB "," njd ":" No live "," qy ": "1019"} */var data = JSON. parse (stdout); console. log (data) ;}}); to get a feel that the result of direct curl is the same as that of running the Script: 3rd, call the shellscript (child_process.exe cFile ()) prepare a shell script first. For example, if I want to connect to a server to change its password, I want to provide IP, user, new pwd, old pwd, create a shell script file change_password.sh :#! /Bin/sh IP = "" NAME = "" PASSWORD = "" NEWPASSWORD = "" while getopts "H: U: P: N: "arg # The colon after the option indicates that the option requires the do case $ arg in H) IP = $ OPTARG; U) NAME = $ OPTARG; P) PASSWORD = $ OPTARG; N) NEWPASSWORD = $ OPTARG ;;?) # When there are unrecognized options, What Is arg? Echo "contains unknown parameters" exit 1 ;; esacdone # obtain useridUSERID = '/usr/bin/ipmitool-I lanplus-H $ IP-U $ NAME-P $ PASSWORD user list | grep root | awk' {print $1} ''# echo $ USERID # change the PASSWORD Based on userid/usr/bin/ipmitool-I lanplus-H $ IP-U $ NAME-P $ password user set PASSWORD $ USERID $ NEWPASSWORD, then I prepare a nodejs file to call this shell script, file_changepwd.js: var callfile = require ('child _ Process'); var ip = '1. 1.1.1 '; var username = 'TE St'; var password = 'pwd'; var newpassword = 'newpwd'; callfile.exe cFile ('change _ password. sh', ['-H', ip,'-U', username, '-p', password,'-n', newpassword], null, function (err, stdout, stderr) {callback (err, stdout, stderr) ;}); it is not convenient to paste the running result here, But I can use personality guarantee, it is tested. After reading the above, it is no suspense to call the python script. In essence, it is to execute the command. 4. Call the python script (the python script itself transmits parameters). Here we insert an additional remark. The following section describes the python parameters: #-*-coding: UTF-8-*-''': Required module: Number of sys parameters: len (sys. argv) Script Name: sys. argv [0] parameter 1: sys. argv [1] parameter 2: sys. argv [2] ''' import sysprint u "Script Name:", sys. argv [0] for I in range (1, len (sys. argv): # The print u "parameter", I, sys. argv [I] I will also prepare a nodejs file to call this python script (I modified py_test.py, see below), file_python.js: var exec = require('child_process'cmd.exe c; var arg1 = 'hello' var arg2 = 'jzhou' exec ('python py_test.py '+ arg1 + ''+ arg2 +'', function (error, stdout, stderr) {if (stdout. length> 1) {console. log ('You offer args: ', stdout);} else {console. log ('you don \'t offer args ');} if (error) {lele.info ('stderr:' + stderr) ;}}); py_test.py contains the following content: #-*-coding: UTF-8-*-import sysprint sys. argv