I am working on a log tail project using node.js but the function process.createChildProcess is not working.Is there any alternative to that?
var sys = require('sys');
var filename = "test.txt";
var process=require('process')
if (!filename)
return sys.puts("Usage: node watcher.js filename");
// Look at http://nodejs.org/api.html#_child_processes for detail.
var tail = process.createChildProcess("tail", ["-f", filename]);
sys.puts("start tailing");
tail.addListener("output", function (data) {
sys.puts(data);
});
// From nodejs.org/jsconf.pdf slide 56
var http = require("http");
http.createServer(function(req,res){
res.sendHeader(200,{"Content-Type": "text/plain"});
tail.addListener("output", function (data) {
res.sendBody(data);
});
}).listen(8000);
Here I am getting error:
var tail = process.createChildProcess("tail", ["-f", filename]);
^
TypeError: process.createChildProcess is not a function
Any help will be appreciated
Article referred: https://thoughtbot.com/blog/real-time-online-activity-monitor-example-with-node-js-and-websocket
You can use spawn for creating a child process in Node.JS.
Here's an example based on your code:
const { spawn } = require('child_process');
/* Some Code Here! */
var tail = spawn("tail", ["-f", filename]);
Related
I am trying to implement a simple web interface where I can see do tail -f of my log file.
Found the below link
https://thoughtbot.com/blog/real-time-online-activity-monitor-example-with-node-js-and-websocket
It seems to be very old and not compatible with modern-day node.j.
I tried to go through node.j` documentation but was unable to fix this.
Child process creation is causing some issues.
var filename = process.argv[2];
if (!filename)
return sys.puts("Usage: node watcher.js filename");
var tail = process.createChildProcess("tail", ["-f", filename]);
console.log("start tailing");
tail.addListener("output", function (data) {
console(data);
});
var http = require("http");
http.createServer(function(req,res){
res.sendHeader(200,{"Content-Type": "text/plain"});
tail.addListener("output", function (data) {
res.sendBody(data);
});
}).listen(8000);
I want to send this tailf'd log to another server, which will be running nodejs app to read this.
Can someone help me out?
I've searched through the whole nodejs documentation, but I haven't found any version where there were process.createChildProcess().
However, you can replace it using the child-process built-in module:
const filename = process.argv[2];
if (!filename){
console.log("Usage: node watcher.js filename");
process.exit(0)
}
const child = require('child-process')
const tail = child.exec("tail", ["-f", filename],{shell:true});
console.log("start tailing");
tail.stdout.on("data", function (data) {
console.log(data);
});
const http = require("http");
http.createServer(function(req,res){
res.writeHead(200,{"Content-Type": "text/plain"});
tail.stdout.on("data", function (data) {
res.write(data);
});
}).listen(8000);
I would like to run an exe in Windows from my Node Webkit app.
I am trying the below code but it is not working.
document.getElementById('play').onclick = function()
{
var spawn = require('child_process').spawn;
var child = spawn(__dirname + '/core.exe', ['/arg1']);
var snd = new Audio("min.wav");
snd.play();
win.minimize();
child.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});
child.stderr.on('data', function (data) {
console.log('stderr: ' + data);
});
child.on('close', function (code) {
console.log('child process exited with code ' + code);
var snd = new Audio("restore.wav");
snd.play();
win.restore();
});
}
Am I getting the path wrong? I need it to be current directory and run the exe with that name and the example arg.
The output SHOULD be a messagebox, but nothing loads.
Managed to figure it out, it wasn't defined because I was using it in browser context. I didn't get the nw.js SDK version for some reason, found that __DIRNAME was undefined. Came up with this solution instead.
var path = require('path');
var nwDir = path.dirname(process.execPath);
var spawn = require('child_process').spawn;
var child = spawn(nwDir + '/app/core.exe', ['/arg1']);
Now working as intended.
In the "Main.js", i use spawn to create a child_process.I want get data from "work.js",but it show error
TypeError: out.clearLine is not a function
If use node work.js in the terminal,it works fine。
It seems like process.stdout does't have function "clearLine" in childProcess.
main.js
var spawn = require('child_process').spawn;
var child = spawn('node',['work.js']);
child.stdout.on('data', function (data) {
console.log('data:',data.toString());
})
child.stderr.on('data', function (data) {
console.log("stderr:", data.toString());
});
work.js
var out = process.stdout;
var idx = 0;
var id = setInterval(()=>{
idx++;
out.clearLine();
out.cursorTo(0);
out.write('workTime:' + idx);
if(idx>3){
clearInterval(id);
console.log();
console.log('end')
}
},100)
It's just a demo, i cant't change the work.js. How can i fix the problem in main.js,thanks
See: node.js stdout clearline() and cursorTo() functions
var readline = require('readline');
function writeWaitingPercent(p) {
//readline.clearLine(process.stdout);
readline.cursorTo(process.stdout, 0);
process.stdout.write(`waiting ... ${p}%`);
}
I am trying to use the image-size module to get the width of an image via URL. I followed their directions explicitly but can't find the "HTTP" module anywhere (which this seems to require).
I'm fairly new to node.js. Here is my code:
var url = require('/usr/lib/node_modules/url');
// var http = require('/usr/lib/node_modules/http'); *** Can't find this to download
var imgSize = require('/usr/lib/node_modules/image-size/');
var imgUrl = 'http://my.nintendo.com/static/images/common/ogp/my-nintendo.png';
var options = url.parse(imgUrl);
http.get(options, function (response) {
var chunks = [];
response.on('data', function (chunk) {
chunks.push(chunk);
}).on('end', function() {
var buffer = Buffer.concat(chunks);
console.log(imgSize(buffer));
});
});
Here is a link to the package on NPM: https://www.npmjs.com/package/image-size
You must require like this at the top of your file:
var url = require('url');
var http = require('http');
var sizeOf = require('image-size');
You have the module installed in your project node_modules folder?
First off, apologies for my lack of knowledge with node, I'm probably making some big noob mistakes. In the following example I'm trying to set a variable to a function that has a continuously changing output, listen for a change in that variable, and output the new results whenever the value of the variable changes. This is the following error that I'm receiving and I don't have a clue what to do about it.
cli.js:15
result.on('data', function(data) {
^
TypeError: Object function () {
runCommand('watch','-n1 ps -ef | grep node');
} has no method 'on'
Here is my example code:
var spawn = require('child_process').spawn;
function runCommand(arg1,arg2) {
var cmd = spawn(arg1,[arg2]);
cmd.stdout.setEncoding('utf8');
cmd.stdout.on('data', function(data) {
return data;
});
}
var result = function() {
runCommand('watch','-n1 ps -ef | grep node');
}
result.on('data', function(data) {
console.log(result);
});
I'm running this on a Linux build.
Instead of returning data from .on('data'.., you should log the results. This should not actually return anything, since it's an async callback.
Also, you do not need to create function to use runCommand, just call the method somewhere in the code. You will also need to note that -n1 and ps -ef | grep node should be separate arguments.
var spawn = require('child_process').spawn;
// Add a callback argument to runCommand.
function runCommand(arg1, arg2, callback) {
// Remove the [] around arg2.
var cmd = spawn(arg1,arg2);
cmd.stdout.setEncoding('utf8');
cmd.stdout.on('data', callback);
}
// Instead of passing one long string as arg2, use an array to pass each argument.
runCommand('watch', [ '-n1', 'ps -ef | grep node' ], function (data) {
// Log the output in the callback.
console.log('Data received: ' + data);
});
After doing some more searching what I needed to do was create a custom event handler. Here's the example I came up with:
var spawn = require('child_process').spawn;
var events = require('events');
var util = require('util');
runCommand = function (arg1,arg2) {
var self = this;
var cmd = spawn(arg1,arg2);
cmd.stdout.setEncoding('utf8');
cmd.stdout.on('data', function(data) {
self.emit('updated', data);
});
}
util.inherits(runCommand, events.EventEmitter);
var result = new runCommand('watch',['date +%s']);
result.on('updated', function(data) {
console.log('command ran');
});