Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

steve@atom:~$ node -v v0.4.2  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 v0.4.2  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?

like image 604
Steve Avatar asked May 01 '11 15:05

Steve


People also ask

How do I run a node script in crontab?

Entry to Run Our Node File Show activity on this post. Now, if you change the node version in your project and update your . nvmrc with the proper version number and pull the new code onto the server, the next time the cron job runs it will run with version of node specified.

What is the use of * * * * * In cron?

It is a wildcard for every part of the cron schedule expression. So * * * * * means every minute of every hour of every day of every month and every day of the week .

How do I schedule a node job?

To use current data in the future you can use binding: const schedule = require('node-schedule'); const date = new Date(2012, 11, 21, 5, 30, 0); const x = 'Tada!' ; const job = schedule. scheduleJob(date, function(y){ console.


1 Answers

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 
like image 129
Dan D. Avatar answered Sep 16 '22 12:09

Dan D.