为什么cron不正确执行我的node.js脚本?

发布于 2025-01-28 09:14:35 字数 878 浏览 2 评论 0原文

我正在尝试从cron运行node.js脚本。它应该读取.json文件,进行API调用,然后写入.json-file的响应。

手动执行时,一切正常。

这确实是工作

sudo node ~/bots/gtaa/index.js

sudo crontab -e

29 9 * * 1-5 /usr/bin/node ~/bots/gtaa/index.js >> ~/gtaa-bot.log 2>&1

grep cron/var/var/log/syslog

May 11 09:29:01 raspberrypi CRON[54381]: (root) CMD (/usr/bin/node ~/bots/gtaa/index.js >> ~/gtaa-bot.log 2>&1)

我的index.js使用local .env-文件

require('dotenv').config({ path: __dirname + '/.env' })

也有 no gtaa-bot.log -file在主目录中。

提出了一个非常类似的问题在这里假如。

I am trying to run a node.js script from cron. It is supposed to read a .json-File, make an API call and then write the response to a .json-File.

When executing manually everything works fine.

This does it's job

sudo node ~/bots/gtaa/index.js

sudo crontab -e

29 9 * * 1-5 /usr/bin/node ~/bots/gtaa/index.js >> ~/gtaa-bot.log 2>&1

grep cron /var/log/syslog

May 11 09:29:01 raspberrypi CRON[54381]: (root) CMD (/usr/bin/node ~/bots/gtaa/index.js >> ~/gtaa-bot.log 2>&1)

My index.js uses the local .env-File

require('dotenv').config({ path: __dirname + '/.env' })

There also is no gtaa-bot.log-File in the home directory.

A very similar question has been asked here but there are no useful answers provided.

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

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

发布评论

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

评论(1

可可 2025-02-04 09:14:35

好吧,我经历了这个问题,最近使用Ubuntu 20.04.4 LTS

最终发生的事情是Cron在不同的环境中运行,并且很可能无法找到正确的节点途径。

有一些方法可以配置这一点,但是我发现一个更容易的解决方案最终只是让Cron执行一个运行NVM和/或节点的bash脚本。

例如,我有此设置每20分钟运行一次:

20 * * * * /bin/bash/ /home/user/nvm_cron.sh

在我的nvm_cron.sh.sh文件中:

# Load nvm
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
nvm use 16.14.0

# Run node program
cd /home/user/folder && node start.js

不要忘记确保nvm_cron.sh.sh文件是可执行的

以这种方式,我可以控制要为脚本运行的节点的版本,而不必担心会弄乱cron。

Well I have experienced this issue, more recently using Ubuntu 20.04.4 LTS

Ultimately what ends up happening is cron runs in a different environment and most likely is unable to find the correct path to node.

There are some ways to configure this, however I found a much easier solution ended up being to just have cron execute a bash script, that runs nvm and/or node.

For example, I have this setup to run every 20 minutes:

20 * * * * /bin/bash/ /home/user/nvm_cron.sh

And in my nvm_cron.sh file:

# Load nvm
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
nvm use 16.14.0

# Run node program
cd /home/user/folder && node start.js

Dont forget to make sure the nvm_cron.sh file is executable

This way I have control over the version of node that I want to run for the script, and don't have to worry about messing with cron.

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