Cron运行Node.js无法正常工作 [英] Cron run Nodejs Not working

查看:91
本文介绍了Cron运行Node.js无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要运行一个sh脚本 send.sh ,如下所示:

  node send.js -q 8435924032>> send.log 

如果我使用crontab将其运行为 * / 2 * * * * /home/app/send.sh
什么也不会输出。



有一封邮件显示抛出错误通过cron进程:

  ReferenceError:未在requestURL(/home/app/getData.js上定义
的承诺:34:16)Object.getData(/home/app/getData.js:15:18)
/home/app/send.js:173:41
.. 。

但是当我直接运行 sh时,它给我正确的输出,并且没有错误send.sh



有人可以帮忙吗?我想每两分钟运行一次程序。
我尝试了PM2,但这种cron功能不起作用。

解决方案

您是否检查了用户根node.js版本?



Crontab默认以root用户身份运行Shell脚本,这意味着它使用的是root用户node.js,而不是您用来运行脚本的节点版本。 / p>

较旧版本的node.js不支持Promise。



如果您具有root特权,则可以检查根用户node.js版本,

  $ sudo su 
$ node -v

,或者在您的node.js脚本中,

  console.log(process.versions); //其中包含运行中的节点版本


I need to run a sh script send.sh as content shown below:

node send.js -q 8435924032 >> send.log

If I run it with crontab as */2 * * * * /home/app/send.sh nothing is outputed.

And there is a mail which shows an error thrown by the cron process:

ReferenceError: Promise is not defined
    at requestURL (/home/app/getData.js:34:16)
    at Object.getData (/home/app/getData.js:15:18)
    at /home/app/send.js:173:41
    ...

However it gives me correct output and NO ERROR when I directly run sh send.sh

Can someone helps? I want to run the program every two minutes. I tried PM2 but such cron feature is not working.

解决方案

Did you check the user root node.js version?

Crontab runs shell script as root by default, that means it's using the root user node.js instead of the node version which you are using to run the script.

Promise is not supported in older version of node.js.

If you have root privilege, you can check root user node.js version by,

$sudo su
$node -v

or, in your node.js script,

console.log(process.versions); //which contains running node version

这篇关于Cron运行Node.js无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆