Organizational Research By

Surprising Reserch Topic

how can you execute a node js script via a cron job

how can you execute a node js script via a cron job  using -'node.js,cron,crontab'

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

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

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

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

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?

asked Sep 18, 2015 by vickeykumar66
0 votes

Related Hot Questions

2 Answers

0 votes

I don't know if changing your relative paths in your script to absolute paths is a good idea
(what happens when your file system changes or you deploy in another environment?)

You could try wrapping it in a shell script, setting some environment variables in the crontab execution. (specifically PATH & NODE_PATH for starters)

Try my suggestion for this similar question:

answered Sep 18, 2015 by amit.gupta
0 votes

Additionally, just put #!/usr/local/bin/node at the top of the script you want to execute. Then it will automatically know to execute the script with node. Make sure the file is executable as well.

answered Sep 18, 2015 by amit.gupta