简体   繁体   English

Node.js 脚本未从 crontab 执行

[英]Node.js script not executing from crontab

I have a crontab entry that is supposed to execute a node.js script like this:我有一个 crontab 条目,它应该像这样执行 node.js 脚本:

*/5 * * * * node /home/campaigns/reporting/UNIT_TESTS/testCron.js > /home/campaigns/reporting/UNIT_TESTS/cron.log

However, it doesn't execute and the log file isn't updated.但是,它不会执行,日志文件也不会更新。 When I run the script manually, everything works though.当我手动运行脚本时,一切正常。 Any ideas??有任何想法吗??

Thank you, Igor谢谢你,伊戈尔

I also had this problem.我也有这个问题。 Seems that after system update on our server the link to node binary is gone from PATH .似乎在我们的服务器上进行系统更新后,指向节点二进制文件的链接从PATH消失了。 Therefore the best solution in this case is always use not node script.js , but full path to binary which in our case is /usr/local/bin/node script.js .因此,在这种情况下,最好的解决方案始终不是使用node script.js ,而是使用二进制文件的完整路径,在我们的例子中是/usr/local/bin/node script.js

I recently ran in to this same problem and was able to solve it with the help of the below blog post.我最近遇到了同样的问题,并且能够在下面的博客文章的帮助下解决它。

Essentially, put the full path to node and your file in the cron job:本质上,将节点和文件的完整路径放在 cron 作业中:

/usr/local/bin/node /var/www/coffee.js

http://www.themechanism.com/voice/2012/08/28/getting-node-js-and-cron-to-play-nicely/ http://www.themechanism.com/voice/2012/08/28/getting-node-js-and-cron-to-play-nicely/

Try making a script with the command:尝试使用以下命令编写脚本:

script.sh : script.sh

#!/usr/bin/env sh
node /home/campaigns/reporting/UNIT_TESTS/testCron.js > /home/campaigns/reporting/UNIT_TESTS/cron.log

and then adding that to cron:然后将其添加到cron:

*/5 * * * * /path/to/script.sh

make sure to make the script executable ( chmod +x script.sh )确保使脚本可执行( chmod +x script.sh

我在Linux中使用nvm install node.js,似乎crontab不知道路径,所以我写了节点的完整路径来执行节点文件。

* * * * * /home/www/.nvm/versions/node/v8.11.1/bin/node /home/www/ss-config/index.js

It's explained pretty well over here .这里解释得很好。

This can happen if your binary (node in this case) is under /usr/local/bin.如果您的二进制文件(本例中为节点)位于 /usr/local/bin 下,则可能会发生这种情况。 By default, crontab looks for the binaries under /usr/bin or /bin.默认情况下,crontab 在 /usr/bin 或 /bin 下查找二进制文件。

Try to actually run the command from the promp > then hit the endpoint and look at the catch message.尝试从提示符 > 实际运行命令,然后点击端点并查看 catch 消息。

For me step 1对我来说第 1 步

/usr/bin/node /var/www/iiiiii.net/server.js

then hit the endpoint (or url) with browser or postman.然后用浏览器或邮递员点击端点(或网址)。

I had the following error because my config did not pick up .env credentials when being run off of crontab.我有以下错误,因为我的配置在运行 crontab 时没有获取.env凭据。

 Access denied for user ''@'localhost' (using password: NO)

It could be any number of errors - but if you test it in this way it will reveal your particular error.它可能是任意数量的错误 - 但如果您以这种方式对其进行测试,它将揭示您的特定错误。

Making soft links like this might be a solution for using node in crontab when it was installed with NVM:当与 NVM 一起安装时,制作这样的软链接可能是在 crontab 中使用 node 的一种解决方案:

$ sudo ln -s "$NVM_DIR/versions/node/$(nvm version)/bin/node" "/usr/local/bin/node"
$ sudo ln -s "$NVM_DIR/versions/node/$(nvm version)/bin/npm" "/usr/local/bin/npm"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM