如何通过 cron 作业执行 Node.js 脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5849402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can you execute a Node.js script via a cron job?
提问by Steve
Quite simply, I have node script that I want to execute once a month.
很简单,我有我想每月执行一次的节点脚本。
30 6 1 * * node /home/steve/example/script.js
But this doesn't work, presumably because of path or the shell the command is being ran under. I've tried the following means of executing node via cron (tested with -v):
但这不起作用,大概是因为路径或命令正在运行的外壳。我尝试了以下通过 cron 执行节点的方法(使用 -v 测试):
steve@atom:~$ node -v
v0.4.2
steve@atom:~$ sh node -v
sh: Can't open node
steve@atom:~$ bash node -v
/usr/local/bin/node: /usr/local/bin/node: cannot execute binary file
steve@atom:~$ /usr/local/bin/node -v
v0.4.2
steve@atom:~$ sh /usr/local/bin/node -v
/usr/local/bin/node: 1: Syntax error: "(" unexpected
steve@atom:~$ bash /usr/local/bin/node -v
/usr/local/bin/node: /usr/local/bin/node: cannot execute binary file
I've ran out of ideas to try, any advice?
我已经没有想法可以尝试了,有什么建议吗?
回答by Dan D.
just provide the full path to node /usr/local/bin/nodein your cron job like:
只需/usr/local/bin/node在您的 cron 作业中提供节点的完整路径,例如:
30 6 1 * * /usr/local/bin/node /home/steve/example/script.js
回答by Mauvis Ledford
Additionally, just put #!/usr/local/bin/nodeat the top of the script you want to execute. Then it will automatically know to execute the script with node. Make sure the file is executable as well.
此外,只需放在#!/usr/local/bin/node要执行的脚本的顶部即可。然后它会自动知道用 node.js 执行脚本。确保该文件也是可执行的。
回答by Curtis Xiao
These answers here saying using absolute path will all cause major problems for running a larger node app!
这些答案在这里说使用绝对路径都会导致运行更大的节点应用程序的重大问题!
Real Complete Solution
真正完整的解决方案
Edit Cron Jobs
编辑 Cron 作业
crontab -e
Find Node Path
查找节点路径
which node
CD into the destination folder, then Change Cron Job according to Node Path and run script
CD进入目标文件夹,然后根据节点路径更改Cron Job并运行脚本
*/2 * * * * cd /home/destination/path && /bin/node index.js
This will then allow you to run a full NodeJS application without all the errors like how using an absolute path for your index.js file.
这将允许您运行完整的 NodeJS 应用程序,而不会出现所有错误,例如如何为 index.js 文件使用绝对路径。
回答by zabusa
in my laptop using Linux mint the given path not working so i used this to get a work around.
在我使用 Linux mint 的笔记本电脑中,给定的路径不起作用,所以我用它来解决。
$ which node
$ which node
$ /usr/bin/nodethis worked for me.
$ /usr/bin/node这对我有用。
回答by Marc Smith
I don't know if changing your relative paths in your script to absolute paths is a good idea
(what happens when your file system changes or you deploy in another environment?)
我不知道将脚本中的相对路径更改为绝对路径是否是个好主意
(当文件系统更改或部署在其他环境中时会发生什么?)
You could try wrapping it in a shell script, setting some environment variables in the crontab execution. (specifically PATH& NODE_PATHfor starters)
您可以尝试将其包装在 shell 脚本中,在 crontab 执行中设置一些环境变量。(具体PATH&NODE_PATH对于初学者)
Try my suggestion for this similar question:
https://stackoverflow.com/a/27823675/608269
试试我对这个类似问题的建议:https:
//stackoverflow.com/a/27823675/608269
回答by imbolc
You can also specify paths to binary files on top of your user crontab like:
您还可以在用户 crontab 之上指定二进制文件的路径,例如:
PATH=/bin:/usr/bin:/usr/local/bin
* * * * * cd your/path && node foo.js
* * * * * cd your/path && npm run bar
回答by Timothy Moody
Use absolute paths for the node alias and the file to be run.
使用节点别名和要运行的文件的绝对路径。
Edit Cron Jobs
编辑 Cron 作业
crontab -e
Entry to Run Our Node File
进入运行我们的节点文件
This will run every minute.
这将每分钟运行一次。
*/1 * * * * * /bin/node /public/test.js
Full Tutorial https://askmacgyver.com/blog/tutorial/how-to-run-node-scripts-from-a-cron-job
完整教程 https://askmacgyver.com/blog/tutorial/how-to-run-node-scripts-from-a-cron-job

