从cron/shellJS重新启动pm2-bcrypt版本错误



我正试图在Ubuntu 18.04中设置一个crontab进程,以定期检查pm2的状态,并在必要时重新启动它。因为我更熟悉JavaScript,所以我决定让crontab进程运行一个节点文件,该文件从pm2读取JSON,以检查pm2中每个应用程序的状态。如果检测到任何问题,JS文件将使用shellJS执行另一个bash脚本。因此:

  1. Crontab
  2. 节点脚本
  3. 如果pm2应用程序出现问题,请执行bash脚本
  4. 删除现有的pm2应用程序并启动它的新实例

然而,当我从JS文件中执行此操作时,我会从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)

这很奇怪。如果我从命令行运行bash脚本,或者如果我直接在命令行中运行命令,我不会得到错误——只有当我从shellJS/node启动它时。

这是从节点调用它的线路:

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

这是restart-pm2.sh文件中的一行:

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

再说一遍,运行这个确切的命令效果很好。

更新:

我尝试直接从cron运行bash脚本,但也收到了错误。因此,驱动错误的显然不是shellJS/node,而是关于cron上下文的一些内容。

我找到了一个解决方案。我注意到,在测试bash脚本时,我需要确保ecosystem.config.js文件中有绝对路径引用,以使其在bash脚本/cron中工作。显然,该文件还需要对节点版本的绝对引用。无论出于何种原因,当从cron调用时,它只需要这个。

因此,在ecosystem.config.js中的应用程序参数中,我添加了--interpreter标志来引用当前版本节点的绝对路径:

const index = {
name: 'index',
script: '/root/myProject/index.js',
...other arguments
interpreter: "/usr/local/bin/node" // added this line
}

最新更新