Node.js 脚本未从 crontab 执行

发布于 2024-12-01 13:27:33 字数 250 浏览 1 评论 0 原文

我有一个 crontab 条目,应该执行如下所示的 node.js 脚本:

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

但是,它不会执行,日志文件也不会更新。当我手动运行脚本时,一切正常。有什么想法吗?

谢谢你, 伊戈尔

I have a crontab entry that is supposed to execute a node.js script like this:

*/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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

朮生 2024-12-08 13:27:33

我也遇到了这个问题。似乎在我们的服务器上进行系统更新后,指向节点二进制文件的链接已从 PATH 中消失。因此,在这种情况下,最好的解决方案始终不是使用 node script.js,而是使用二进制文件的完整路径,在我们的例子中是 /usr/local/bin/node script.js >。

I also had this problem. Seems that after system update on our server the link to node binary is gone from 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.

晨曦慕雪 2024-12-08 13:27:33

我最近遇到了同样的问题,并在下面的博客文章的帮助下解决了它。

本质上,将节点和文件的完整路径放入 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/

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:

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

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

太阳男子 2024-12-08 13:27:33

尝试使用以下命令创建脚本:

script.sh

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

然后将其添加到 cron:

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

确保使脚本可执行(chmod +x script.sh

Try making a script with the command:

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:

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

make sure to make the script executable (chmod +x script.sh)

旧人 2024-12-08 13:27:33

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

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

I'm using nvm install node.js in Linux, and seems the crontab don't know the path well, so I wrote the full path of node to execute the node file.

* * * * * /home/www/.nvm/versions/node/v8.11.1/bin/node /home/www/ss-config/index.js
凌乱心跳 2024-12-08 13:27:33

此处对此进行了很好的解释。

如果您的二进制文件(在本例中为节点)位于 /usr/local/bin 下,则可能会发生这种情况。默认情况下,crontab 在 /usr/bin 或 /bin 下查找二进制文件。

It's explained pretty well over here.

This can happen if your binary (node in this case) is under /usr/local/bin. By default, crontab looks for the binaries under /usr/bin or /bin.

宛菡 2024-12-08 13:27:33

像这样制作软链接可能是在 crontab 中使用 Node 与 NVM 一起安装时的解决方案:

$ 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"

Making soft links like this might be a solution for using node in crontab when it was installed with NVM:

$ 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"
风尘浪孓 2024-12-08 13:27:33

尝试从提示符中实际运行命令 >然后到达端点并查看捕获消息。

对我来说,第 1 步

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

然后使用浏览器或邮递员点击端点(或 url)。

我遇到以下错误,因为我的配置在运行 crontab 时未获取 .env 凭据。

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

它可能是任意数量的错误 - 但如果您以这种方式测试它,它将揭示您的特定错误。

Try to actually run the command from the promp > then hit the endpoint and look at the catch message.

For me step 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.

 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文