javascript - Node's spawn() silently failing when called from a forever script scheduled on boot -


this kind of doozy. issue server related , first recourse askubuntu on here.

i'm trying have crontab or rc.local or init.d start forever script on boot. attaches server port can ping information , have run headless browser me.

that said, seems i'm unable response node.js's spawn():

var casper_path = '/home/ubuntu/dev/casperjs/bin/casperjs'; // actual binary location, not symlink var scripts_path = '/home/custom_user/endpoints/server.js';  var filename = req.body.source + '_' + req.body.type + '.coffee'; // looks like: mysource_my_scrape_type.coffee var scrapeid = 'test_scrape'; var user = 'user123'; var pass = 'pass123'; if (fs.existssync(scripts_path + filename)) {   // if file in place, spawn casperjs   var sp = spawn(casper_path,      [scripts_path + filename, '--ssl-protocol=any', '--user='+user, '--scrapeid='+scrapeid, '--pass='+pass],      { detached: true },      function (err, stdout, stderr) {});   sp.stdout.on('data', function(data) { console.log('stdout', data.tostring('utf8')); });   sp.stderr.on('data', function(data) { console.log('stderr', data.tostring('utf8')); });   sp.stdout.on('close', function(code) { console.log('close', code); });   res.send({ scheduled: true, key: scrapeid }); } else {   res.send({ scheduled: false, error: 'incorrect source, type or script missing.' }); } 

before added phantomjs_executable env crontab or rc.local (doesnt seem matter no matter user level), stdout useful:

stdout fatal: [errno 2] no such file or directory; did install phantomjs?

close false

now environment var there, there no output @ after spawn().

mind you, casper starts fine if user (of privilege level) runs node/forever bash.

how can see why spawn() failing?

this looks combo-bug between forever, spawn , casperjs (maybe phantomjs). able reproduce problem, here is full code of test application.

you didn't show full code, guess have express application , there special url run casperjs script.

i build simple app , behaved way:

  • just start app node script.js (script.js express app runs casperjs script in server.js) - works ok, renders response , writes output child process event handlers console
  • start app root init.d script - doesn't work, once child spawned, no event handlers triggered
  • start app root init.d script, replace casperjs echo - same, doesn't work (see, here have problem forever running root, spawn , echo)
  • start app regular user (not root) init.d, replace casperjs 'echo' - works, event handlers triggered, here sure issue solved, ... :(
  • start app regular user (not root) init.d, put casperjs - doesn't work again, event handlers not triggered

the practical solution use pm2, did this:

# install pm2 sudo npm install -g pm2 # generate init.d scripts pm2 # command fail, hint correct format sudo pm2 startup ubuntu # in folder application pm2 start script.js # remember application pm2 save # useful # sudo service stop/start/restart pm2 # pm2 stop/start/restart script 

now pm2 start automatically system , launch application. works, child process event handlers triggered.


Comments