Running node script with crontab

huangapple go评论77阅读模式
英文:

Running node script with crontab

问题

以下是翻译好的内容:

我正在尝试使用crontab来运行一个Node脚本。我首先尝试了类似以下的操作,以确保crontab正常工作(我正在努力让命令正常工作,然后再更改crontab,以便它不会每分钟运行)。

上面的crontab工作正常。问题在于,当我尝试使用Node时,它不会在crontab中运行。运行which node命令,我得到的是/usr/bin/node。以下是我尝试过的方法。

谢谢!

* * * * * cd /path/to/script && node script.js

* * * * * cd /path/to/project && npm start(运行npx tsc && node build/script.js)

* * * * * cd /path/to/script && node script.js > test.txt(文件生成为空,尽管脚本中有console.log)

* * * * * node /path/to/script/script.js

* * * * * echo test > test.txt && node /path/to/script/script.js(文件生成)

此外,我还尝试了将node替换为/usr/bin/node的所有上述命令。如果我手动运行这些命令中的任何一个,它都会执行程序。

英文:

I'm trying to run a node script with crontab. I've tried first doing things like

* * * * * echo test > test.txt

to be sure crontab works (I'm trying to make the command work and then I'll change the crontab to something different so it doesn't run every minute).

The crontab above works. The thing is, when I try to use node, it doesn't run with the crontab. Running which node I get /usr/bin/node Here are the things I've tried.

Thanks!

* * * * * cd /path/to/script && node script.js

* * * * * cd /path/to/project && npm start (runs npx tsc && node build/script.js)

* * * * * cd /path/to/script && node script.js > test.txt (file is generated empty, even though, script has console.log)

* * * * * node /path/to/script/script.js

* * * * * echo test > test.txt && node /path/to/script/script.js (file gets generated)

Also I've tried all of the above replacing node by /usr/bin/node.

If I run any of these commands manually, it executes the program.

答案1

得分: 1

在经过很长时间的研究和测试了很多东西之后,我意识到问题出在执行sudo crontab -e来设置定时任务时。我改为执行以下命令来修复它:

sudo crontab -u 用户名 -e

英文:

After going over a long time and testing a lot of stuff, I realized the issue was doing sudo crontab -e to set the crontab. I fixed it after running instead

sudo crontab -u username -e

huangapple
  • 本文由 发表于 2023年1月6日 01:35:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75022296.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定