从bash脚本运行节点

时间:2021-01-07 00:08:59

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确实有什么问题吗?或者node有特定的方法来做这件事吗?我使用了/bin/env,这样无论版本是什么,我都可以拥有“node”的适当形式。

3 个解决方案

#1


33  

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。错误消息来自node,看起来node试图运行命令/var/ node/assets/js/update.s

I would make the shebang line specify bash rather than node.

我将使shebang行指定bash而不是节点。

The top line

最上面的一条线

#!/usr/bin/env node

means that what follows should be JavaScript code, not bash.

意味着接下来应该是JavaScript代码,而不是bash。

#2


9  

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.

然后在第二行中,您将提供命令节点/var/node/assets/js/update到节点进程的js。

How about this:

这个怎么样:

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

#3


-2  

How about this?

这个怎么样?

#!/bin/bash

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

#1


33  

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。错误消息来自node,看起来node试图运行命令/var/ node/assets/js/update.s

I would make the shebang line specify bash rather than node.

我将使shebang行指定bash而不是节点。

The top line

最上面的一条线

#!/usr/bin/env node

means that what follows should be JavaScript code, not bash.

意味着接下来应该是JavaScript代码,而不是bash。

#2


9  

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.

然后在第二行中,您将提供命令节点/var/node/assets/js/update到节点进程的js。

How about this:

这个怎么样:

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

#3


-2  

How about this?

这个怎么样?

#!/bin/bash

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