0

I'm trying to set up a crontab process in Ubuntu 18.04 to periodically check on the status of pm2 and restart it if necessary. Because I'm more familiar with JavaScript, I decided to have the crontab process run a node file that gets a JSON readout from pm2 to check the status of each app within pm2. If any issues are detected, the JS file will executes another bash script using shellJS. So it goes:

  1. Crontab
  2. Node script
  3. If there is an issue with the pm2 app, execute bash script
  4. Delete existing pm2 app and start a fresh instance of it

However, when I do this originating from the JS file, I get the following error originating from bcrypt:

Error: The module '/root/myProject/node_modules/bcrypt/lib/binding/bcrypt_lib.node'
was compiled against a different Node.js version using
NODE_MODULE_VERSION 67. This version of Node.js requires
NODE_MODULE_VERSION 57. Please try re-compiling or re-installing
the module (for instance, using `npm rebuild` or `npm install`).
    at Object.Module._extensions..node (module.js:681:18)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/root/myProject/node_modules/bcrypt/bcrypt.js:6:16)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/root/myProject/models/User.js:3:16)
    at Module._compile (module.js:652:30)

It's very bizarre. If I run the bash script from the command line or if I run the command directly in the command line, I don't get the error - only when I initiate it from shellJS/node.

Here's the line to call it from node:

shell.exec('/var/scripts/restart-pm2.sh')

Here's the line in the restart-pm2.sh file:

pm2 start /root/ecosystem.config.js --only index

And again, running that exact command works fine.

UPDATE:

I tried running the bash script directly from cron and I also get the error. So it's apparently not shellJS/node that are driving the error but something about the cron context.

SuperCodeBrah
  • 2,874
  • 2
  • 19
  • 33
  • Do you happen to be using a node version manager such as `n`, `nvm`, or `nvs`? – fardjad Oct 25 '19 at 23:36
  • I was able to find it and added an answer. I'm not sure what other version of node it could referencing other than what's at `/usr/local/bin/node` but that seems to solve it. – SuperCodeBrah Oct 25 '19 at 23:53

1 Answers1

0

I figured out a solution. I noticed when testing the bash script that I needed to make sure I had absolute path references in the ecosystem.config.js file to make it work from the bash script/cron. Apparently, that file also needs an absolute reference to the node version. For whatever reason, it only needs this when called from cron.

So in the app arguments in ecosystem.config.js, I added the --interpreter flag to reference the absolute path of the current version of node:

const index = {
    name: 'index',
    script: '/root/myProject/index.js',

    ...other arguments

    interpreter: "/usr/local/bin/node" // added this line
}
SuperCodeBrah
  • 2,874
  • 2
  • 19
  • 33