node.js 从 bash 脚本运行节点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16509848/
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
Running node from a bash script
提问by Rogue
Quite simply, I'm attempting to automate running a nodejs script using cron, however the script itself doesn't seem to be able to run the file. My script is simple:
很简单,我正在尝试使用 cron 自动运行 nodejs 脚本,但是脚本本身似乎无法运行该文件。我的脚本很简单:
#!/usr/bin/env node
node /var/node/assets/js/update.js
However, in running this, it returns that the beginning of the pathing is incorrect:
但是,在运行它时,它返回路径的开头不正确:
/home/dev/update.sh:2
node /var/node/assets/js/update.js
^^^
SyntaxError: Unexpected token var
at Module._compile (module.js:439:25)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:901:3
Is there something actually wrong with the bash, or does node have a specific way of doing this? I used /bin/env so that I could have the proper form of "node" regardless of version.
bash 是否真的有问题,或者节点是否有特定的方法来做到这一点?我使用了 /bin/env 以便无论版本如何,我都可以拥有正确形式的“节点”。
回答by Ray Toal
It looks like you are trying to run node from within node. The error message came from node and it looks like node was trying to run the command /var/node/assets/js/update.js.
看起来您正在尝试从节点内运行节点。错误消息来自节点,看起来节点正在尝试运行命令/var/node/assets/js/update.js。
I would make the shebang line specify bash rather than node.
我会让shebang行指定bash而不是node。
The top line
顶线
#!/usr/bin/env node
means that what follows should be JavaScript code, not bash.
意味着接下来应该是 JavaScript 代码,而不是 bash。
回答by stackunderflow
You are already running node on the first line in an unmodified environment.
您已经在未修改的环境中的第一行运行节点。
then on the second line you supply the command node /var/node/assets/js/update.jsto that node process.
然后在第二行node /var/node/assets/js/update.js向该节点进程提供命令。
How about this:
这个怎么样:
#!/usr/bin/bash
node /var/node/assets/js/update.js
回答by Daniel Viedma
How about this?
这个怎么样?
#!/bin/bash
node /var/node/assets/js/update.js

