Node spawn () incessantly when called from an eternal script scheduled at boot

This is kind of doozy. This problem is most likely server related, and so my first call was AskUbuntu here .

I am trying to run crontab or rc.local or init.d to run the forever script on boot. He attaches the server to a port, which I can ping with some information, and launched the headless browser for me.

However, it seems that I cannot get the answer from Node.js spawn() :

 var CASPER_PATH = '/home/ubuntu/dev/casperjs/bin/casperjs'; // actual binary location, not a 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 is 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 the script is missing.' }); } 

Before adding PHANTOMJS_EXECUTABLE env to crontab or rc.local (it doesn't seem to matter, regardless of user level), stdout was useful:

stdout Fatal: [Errno 2] There is no such file or directory; did you install phantomjs?

close false

Now that the var environment exists, there is no output after spawn() .

Remember that Casper starts just fine if a user (of any privilege level) starts node / forever from bash.

How can I understand why spawn() not working?

+5
source share
2 answers

It looks like a combo bug between the ages, caviar and casperjs (maybe phantomjs). I was able to reproduce your problem, here is the full code of my test application.

You did not provide the full code, so I assume that you have an express application, and there is a special URL to run the casperjs script.

I am creating a simple application like this, and it behaved like this:

  • Just run the application with node script.js ( script.js is an express application that runs the casperjs script in server.js ) - it works fine, displays the response and writes the output of the event handlers of the child processes to the console
  • Run the application with administrator privileges using init.d script - does not work, after the child is born, no event handlers are launched
  • Run the application with administrator privileges using init.d script, replace casperjs with echo - the same thing does not work (see here, we have this problem, only with forever , running as root, spawn and echo )
  • Run the application as a regular user (not root) with init.d, replace casperjs with "echo" - it works, event handlers are started, here I am almost sure that the problem is solved, but ...: (
  • Start the application as a regular user (not root) using init.d, return casperjs back - it does not work again, event handlers do not start

A practical solution to this problem for using pm2 , I did this:

 # install pm2 sudo npm install -g pm2 # generate init.d scripts for pm2 # this command will fail, but hint about the correct format with sudo pm2 startup ubuntu # do this in the folder with your application pm2 start script.js # remember your application pm2 save # also useful # sudo service stop/start/restart pm2 # pm2 stop/start/restart script 

Now pm2 will automatically start with the system and launch your application. Everything works, event handlers of the child process are started.

+2
source

I completely did not understand your requirement. But I have a similar situation with a server without an Ubuntu header.

what i'm trying to do here is what i did

First, how is my crontab?

 crontab -u USER -e @reboot exec sudo -u USER /bin/bash /home/USER/SHELL_SCRIPT.sh 

See here, I am really running a shell script, not a node server

Now inside this shell script (SHELL_SCRIPT.sh)

  #! /bin/bash # SHELL_SCRIPT.sh cd /home/USER/ /home/USER/.npm-packages/bin/forever start -p /home/USER -a -d --watch false --pidFile /home/USER/forever.pid -l /home/USER/forever.log -o /home/USER/forever.out -e /home/USER/forever.err /home/USER/MY_NODE.js 

and even inside my MY_NODE.js I follow the absolute path, I just ignore $ PATH and don't use this.

Inside this node server, I am making 100 from spawn

Now I did it about 2 years ago, so if you ask me why, I can’t answer

0
source

Source: https://habr.com/ru/post/1241788/


All Articles