0

I am working on a node script where I will be opening and closing the browser using child process but its opening the browser but not able to close it on pressing Ctrl + C.

Here's my code:

const { kill } = require('process');

var start = (process.platform == 'darwin'? 'open': process.platform == 'win32'? 'start': 'xdg-open');
//open browser
var url = 'https://www.google.com';
var cmd = start + ' ' + url;
var child = require('child_process').exec(cmd, function(error, stdout, stderr) {
    if (error) {
        console.log('exec error: ' + error);
    }
});

process.on('SIGINT', function() {
    kill(child.pid, 'SIGINT');
    fs.writeFile(./scraper.js)
    process.exit();
});

scarper.js

const cheerio = require('cheerio');
const request = require('request-promise');
const fs = require('fs');


const link="https://www.imdb.com/chart/top/?ref_=nv_mv_250";

(async()=>{
    const html = await request(link);
    const $ = cheerio.load(html);
    const table = $('.lister-list');
    const rows = table.find('tr');
    const data = [];
    for(let i=0;i<rows.length;i++){
        const row = rows.eq(i);
        const cols = row.find('td');
        const rank = i+1;
        const title = cols.eq(1).find('a').text().trim();
        const year = cols.eq(1).find('span').text().trim();
        const rating = cols.eq(2).text().trim();
        const votes = cols.eq(4).text();
        const image = cols.eq(1).find('img').attr('src');
        const movie = {
            rank: rank,
            title: title,
            year: year,
            rating: rating,
            votes: votes,
            image: image
        };
        data.push(movie);
    }
    console.log(data);
    fs.writeFile('./data.json',JSON.stringify(data));
})();

I am working on a node.js app where I am using socket.io to send data to multiple clients but the socket is only able to send data to one client i.e if I open my webpage in two tabs its not working in both the tabs but when I open just 1 tab of webpage it is able to transmit the data.I dont know why? Can someone help,Here's my code:

rudeTool
  • 526
  • 1
  • 11
  • 25
  • I'm pretty sure `open` and `start` are non-blocking commands, i.e. the child processes for them will already have died by the time you call SIGINT. You will have to get the process that those commands opened internally somehow. However this will work fine on Linux. – 101arrowz Aug 06 '21 at 07:15
  • The exec callback is fired as soon the programm/comand has exited/completed what it should do. Therefore is no process/pid anymore available when you try to kill it with "kill". – Marc Aug 06 '21 at 07:26
  • So is there any workaround for this? – rudeTool Aug 06 '21 at 07:28
  • but when I console.log(child.pid),it does print a pid – rudeTool Aug 06 '21 at 07:32
  • @rudeTool it does print the pid because you created/spawned a child process with that pid. that does not mean the programm cant exit before your call kill or want to do anything with it. Node can spawn a process, and that process can exit immediatly. No matter what you want to do witht he pid/process. – Marc Aug 06 '21 at 07:33
  • So how do I make it not exit before I call it? @Marc – rudeTool Aug 06 '21 at 07:34
  • You cant. Thats like call/spawn "exit" from node. – Marc Aug 06 '21 at 07:36

1 Answers1

0

try to use

   setTimeout((function() {
        return process.abort();
    }), 5000);

or try

setTimeout((function() {
    return process.kill(process.pid);
}), 5000);
Zubair Saif
  • 1,106
  • 1
  • 14
  • 29