Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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? I used /bin/env so that I could have the proper form of "node" regardless of version.

like image 334
Rogue Avatar asked May 12 '13 16:05

Rogue


People also ask

How do I run a shell script in node?

Node. js can run shell commands by using the standard child_process module. If we use the exec() function, our command will run and its output will be available to us in a callback. If we use the spawn() module, its output will be available via event listeners.


1 Answers

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.

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

The top line

#!/usr/bin/env node 

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

like image 160
Ray Toal Avatar answered Sep 17 '22 20:09

Ray Toal