如何通过 cron 作业执行 Node.js 脚本? [英] How can you execute a Node.js script via a cron job?

查看:20
本文介绍了如何通过 cron 作业执行 Node.js 脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很简单,我有一个想要每月执行一次的节点脚本.

Quite simply, I have node script that I want to execute once a month.

30 6 1 * * node /home/steve/example/script.js

但这不起作用,大概是因为路径或命令正在运行的shell.我尝试了以下通过 cron 执行节点的方法(使用 -v 测试):

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):

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?

推荐答案

只需在您的 cron 作业中提供节点 /usr/local/bin/node 的完整路径,例如:

just provide the full path to node /usr/local/bin/node in your cron job like:

30 6 1 * * /usr/local/bin/node /home/steve/example/script.js

这篇关于如何通过 cron 作业执行 Node.js 脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆