简体   繁体   English

从bash脚本运行节点

[英]Running node from a bash script

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. 很简单,我试图使用cron自动运行nodejs脚本,但是脚本本身似乎无法运行该文件。 My script is simple: 我的脚本很简单:

#!/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? bash实际上有什么问题吗,还是节点有特定的方法来执行此操作? I used /bin/env so that I could have the proper form of "node" regardless of version. 我使用了/ bin / env,这样无论版本如何,我都可以使用“ node”的适当形式。

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 . 该错误消息来自node,并且看来node正在尝试运行命令/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。

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.js to that node process. 然后在第二行中,向该节点进程提供命令node /var/node/assets/js/update.js

How about this: 这个怎么样:

#!/usr/bin/bash
node /var/node/assets/js/update.js

How about this? 这个怎么样?

#!/bin/bash

node /var/node/assets/js/update.js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM