0

I'm trying to automate a Node.js file to run on schedule. But I can't get it to work.

I'm using root user.

This is the path to get to the file location from login: nodejs_projects/amazon_search_v2

Here is pwd output from the login location:

root@project:~# pwd
/root

And this is the script i'm adding in crontab :

0 4,12,20 * * * node nodejs_projects/amazon_search_v2/searchItemsApi.js  >/dev/null 2>&1

What am i'm missing here?

yoni
  • 129
  • 9

2 Answers2

2

This one worked for me:

* 0,8,16 * * * cd ~/nodejs_projects/amazon_search_v2/ && /usr/bin/node searchItemsApi.js >/dev/null 2>&1

As described here: Link

In Curtis Xiao answer. Using which node to find the node executable path and cd to get into the file folder and prevent relative path issues.

yoni
  • 129
  • 9
0
  • You have to provide a full path to node /usr/local/bin/node like this.

0 4,12,20 * * * /usr/local/bin/node nodejs_projects/amazon_search_v2/searchItemsApi.js >/dev/null 2>&1

  • Better way is to use node-cron library.
const cron = require('node-cron');
 
cron.schedule('0 4,12,20 * * *', function(){
  // task goes here
});
koko-js478
  • 1,703
  • 7
  • 17
  • Thanks, but I had memory issues using node-cron so i want to try a regular cron job to see if that is the issue. – yoni Oct 04 '20 at 09:48