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:
- Crontab
- Node script
- If there is an issue with the pm2 app, execute bash script
- 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.