CasperJS/PhatomJS running shell commands - node.js

For the last few days I have been struggling with running shell commands from CasperJS/PhantomJS.
I am running simple unix sed on a file in node, which runs just fine:
var sys = require('sys')
var exec = require('child_process').exec;
var child;
// executes `sed`
child = exec("sed -i -e '1,1000d' file.name", function (error, stdout, stderr) {
sys.print('stdout: ' + stdout);
sys.print('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
});
But whenever I run same with CasperJS, it just runs forever, not executing the shell command. Maybe someone could shed some light on this.
I did actually make it work through spawn and execFile functions from PhatomJS, but the issue is that it does not work with large files over 300MB.

Related

Unable to trigger NodeJS based BATCH/Exe file using jenkins

Im trying to execute the my batch/exe file through NodeJS script by using the:
var child_process = require('child_process');
i.e.
child_process.execFile For exe
child_process.exec For Batch file
When I'm trying to execute my scripts by using followings TWO method:
Triggering via CMD it will be get executed successfully.
Triggering via Jenkins it will NOT get executed.
In both cases directory is same.
I have been using the followings function for this purpose:
exports.exec_exe_file = exec_exe_file = function(exe)
//child_process.execFile(exe, function(error, stderr, stdout) {
child_process.exec(exe, function(error, stderr, stdout) { if (error) {
console.error('stderr', stderr); throw error; } //console.log('stdout',
stdout); });
}
Called as:
var autoit = __dirname + "\\autoit\\start_AutoitExe.bat";
//var autoit = __dirname + '\\autoit\\Script.exe';
exec_exe_file(autoit);

nodejs command line module not giving stdout

I have set up a command line node module and when I run it one of the tasks is to start up the server and log to the console. However I find that although it starts up my server fine, it does not send the output to the console.
#! /usr/bin/env node
var userArgs = process.argv.slice(2);
var searchPattern = userArgs[0];
if(userArgs[0] === "start"){
var exec = require('child_process').exec;
exec('node ./server.js',
function(err, stdout, stderr) {
console.log('stdout: ', stdout);
console.log('stderr: ', stderr);
if (error !== null) {
console.log('exec error: ', error);
}
}
);
}
So if I npm link my module then run mymodule start it starts the server fine but as I mentioned no output to the console.
Whereas if I run simply node server.js I get the output which is server listening on http://localhost:5000.
From the documentation, regarding the callback you are passing to 'exec'
'callback' - Function called with the output when process terminates
From what I understand, the process has to be terminated before you see 'stdout:' and 'stderr:' (you would have to stop the node process).

How to use wildcards in nodejs child_process.exec

Why does this not work? How to do it right?
var exec = require("child_process").exec;
exec("ls data/tile_0_{0..63}.jpg", function(error, stdout, stderr){
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr)}
);
// stdout:
// stderr: ls: cannot access data/tile_0_{0..63}.jpg: No such file or directory
In bash terminal this lists all files like tile_0_0.jpg, tile_0_1.jpg, etc.
This executed from node works as expected but does not do the thing I want.
ls data/tile_61_[0-9].jpg
Please help me ...
I'm using Linux Mint 14.
Why not do
var _ = require('underscore'),
fs = require('fs'),
files = fs.readdirSync('data');
var filtered = _.filter(files, function(filename){
return filename.indexOf('tile_0_') == 0;
});

Can Node.js launch scripts on the server?

Can Node.js launch scripts on the server it is installed on? Scripts like bash scripts or PHP scripts, for example to resize pictures?
If so, how how is it? Can you point me to a documentation page, please?
Thanks,
Dan
Yes it is possible. The following give a demonstration:
http://www.dzone.com/snippets/execute-unix-command-nodejs
http://groups.google.com/group/nodejs/browse_thread/thread/e3d00bb0e48dd760?pli=1
You can also perform tasks such as spawning child processes, and clustering.
Executing a unix command:
var exec = require('child_process').exec;
child = exec("something", function (error, stdout, stderr) {
sys.print('stdout: ' + stdout);
sys.print('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
});
See Node.js Documentation for more: http://nodejs.org/api/child_process.html

Calling Make file in nested folder via Node.js

I have a complicated MakeFile file I want to call as part of my node.js app, the make file is a couple of directories deep from root. I know I need to spawn a 'make' chile process but moving node into the sub directory to call the make im not so sure about.
Сould you move the make to the nested folder? I would try something like
var util = require('util'),
exec = require('child_process').exec,
child;
child = exec('cd samples/nestedmake && make',
function (error, stdout, stderr) {
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
});
(copied with minimal changes from Node Child Processes documentation).

Resources