cron crontab node.js

How can you execute a Node.js script via a cron job?

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):

[email protected]:~$ node -v
[email protected]:~$ sh node -v
sh: Can't open node
[email protected]:~$ bash node -v
/usr/local/bin/node: /usr/local/bin/node: cannot execute binary file
[email protected]:~$ /usr/local/bin/node -v
[email protected]:~$ sh /usr/local/bin/node -v
/usr/local/bin/node: 1: Syntax error: "(" unexpected
[email protected]:~$ 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?

just provide the full path to node /usr/local/bin/node in your cron job like:

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