Nodejs Childexec execute command for each line in file but wait for the first command to exit before running the next

佐手、 提交于 2021-02-10 05:11:50

问题


I am using nodejs to communicate with a casperjs script i have made. first of all i will tell you what my Casperjs script does. i have set it up with command line input. i the run this command

casperjs script.js "Inputdata1" "inputdata2"

this script then executes and visits one of my servers submits the input data 1 & 2.

then waits for a server response and rights a line to one of 10 text files depending on the result the script gets from my server

this then exit. this casperjs script works fine no problems.

now i am building a nodejs script to run the casperjs script over and over again

my nodejs script is suppose to read a text-file using readline.js then for each line execute the Casperjs command using the data inside.

the problem is my script does every execution at once and i get no results in my text files from casperjs

i need my nodejs script to execute the first line wait for Casperjs to exit then and only then move on to the next line and execute that with Casperjs

and also i need the actual casperjs script to work as usual

here is my nodejs script -- i have been at this for days trying different code different modules but cannot solve it so any help is greatly appreciated

var fs = require('fs'),
    sleep = require('sleep'),
    readline = require('readline');

var rd = readline.createInterface({
    input: fs.createReadStream('file.txt'),
    output: process.stdout,
    terminal: false
});

rd.on('line', function(line) {



var exec = require('child_process').exec;
var child = exec('casperjs check.js ' + line );

    child.on('close', function(code) {
        console.log('closing code: ' + code);

    });

});

回答1:


You are looking for sequential loop. That is, execute a next step after previous is finished.

The difficulty comes, as Node.js is asynchronous, and there is very little sequential functionality out of the box.

There are two options.

Recursive callbacks

var fs = require('fs'),
    sleep = require('sleep'),
    exec = require('child_process').exec,
    readline = require('readline');

var rd = readline.createInterface({
    input: fs.createReadStream('file.txt'),
    output: process.stdout,
    terminal: false
});

var lines = [];
rd.on('line', function(line) {
    lines.push(line);
});
rd.on('end', function() {
    var i=0;
    if(i < lines.length) {
        exec('casperjs check.js ' + lines[i], function execCallback(code) {
            console.log('closing code: ' + code);
            i++;
            if(i < lines.length) exec('casperjs check.js ' + lines[i], execCallback);
        });
    }
});

Asynchronous library with sequencial support

There are many options, for sake of example, let's look at async.

var fs = require('fs'),
sleep = require('sleep'),
exec = require('child_process').exec,
async = require('async'),
readline = require('readline');

var rd = readline.createInterface({
input: fs.createReadStream('file.txt'),
output: process.stdout,
terminal: false
});

var q = async.queue(function (task, callback) {
var child = exec('casperjs check.js ' + task.line );

child.on('close', function(code) {
    callback(code);
});
}, 1);

rd.on('line', function(line) {
q.push({line: line}, function (code) {
    console.log('closing code: ' + code + line);
    JSON.stringify();
});

});

Did not test, so you might need to fix the typos if any.



来源:https://stackoverflow.com/questions/28022770/nodejs-childexec-execute-command-for-each-line-in-file-but-wait-for-the-first-co

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!