Skip to content

Instantly share code, notes, and snippets.

@eugenehp
Created June 26, 2012 13:46
Show Gist options
  • Save eugenehp/2995867 to your computer and use it in GitHub Desktop.
Save eugenehp/2995867 to your computer and use it in GitHub Desktop.
Node.js spawn and control Google Chrome
var express = require('express');
var cp = require('child_process');
var app = express.createServer();
var chrome_count = 0;
app.get('/', function(req, res){
startChrome(chrome_count++,9222,function chromeStared(err,id,chrome){
res.send('Chrome '+id+' Started\r\n');
console.log('Simulating data parsing');
setTimeout(function(){
console.log('Shutdown chrome '+id);
chrome.kill('SIGHUP');
},10000);
});
});
var port = 3000;
app.listen(port);
console.log('Server is listening on port ' + port);
function startChrome(id,port,callback){
var terminal = cp.spawn('bash');
var chrome = {};
terminal.on('exit', function (code) {
//console.log('child process exited with code ' + code);
console.log('Starting chrome');
chrome = cp.spawn('/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome',[
'--remote-debugging-port='+port,
'--user-data-dir=/Volumes/DATA/repos/scrapper/userdata',
'http://www.patrickhyundai.com/browse-new-hyundais.aspx'
]);
callback(null,id,chrome);
});
setTimeout(function() {
console.log('Sending stdin to terminal');
//terminal.stdin.write('echo "Hello $USER"');
terminal.stdin.write('rm -rf /Volumes/DATA/repos/scrapper/userdata'+'\n');
terminal.stdin.write('mkdir /Volumes/DATA/repos/scrapper/userdata'+'\n');
terminal.stdin.write('touch "/Volumes/DATA/repos/scrapper/userdata/First Run"'+'\n');
terminal.stdin.write('chmod 777 "/Volumes/DATA/repos/scrapper/userdata/First Run"'+'\n');
terminal.stdin.end();
}, 1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment