6

I have a crontab entry that is supposed to execute a node.js script like this:

*/5 * * * * node /home/campaigns/reporting/UNIT_TESTS/testCron.js > /home/campaigns/reporting/UNIT_TESTS/cron.log

However, it doesn't execute and the log file isn't updated. When I run the script manually, everything works though. Any ideas??

Thank you, Igor

IgorGanapolsky
  • 26,189
  • 23
  • 116
  • 147

7 Answers7

19

I also had this problem. Seems that after system update on our server the link to node binary is gone from PATH. Therefore the best solution in this case is always use not node script.js, but full path to binary which in our case is /usr/local/bin/node script.js.

X-Istence
  • 16,324
  • 6
  • 57
  • 74
Alexey Kamenskiy
  • 2,888
  • 5
  • 36
  • 56
11

I recently ran in to this same problem and was able to solve it with the help of the below blog post.

Essentially, put the full path to node and your file in the cron job:

/usr/local/bin/node /var/www/coffee.js

http://www.themechanism.com/voice/2012/08/28/getting-node-js-and-cron-to-play-nicely/

Anconia
  • 3,888
  • 6
  • 36
  • 65
  • Another helpful tip in that blog post is giving absolute, rather than relative, paths to the node modules you're using. That, in conjunction with the above suggestion, fixed my issue. – nickcoxdotme Sep 27 '15 at 18:53
9

Try making a script with the command:

script.sh:

#!/usr/bin/env sh
node /home/campaigns/reporting/UNIT_TESTS/testCron.js > /home/campaigns/reporting/UNIT_TESTS/cron.log

and then adding that to cron:

*/5 * * * * /path/to/script.sh

make sure to make the script executable (chmod +x script.sh)

Daniel
  • 30,896
  • 18
  • 85
  • 139
  • 2
    Well, that got me farther along. However, instead of logging to my file it outputs the following on the command line: "You have new mail in /var/spool/mail/root". Also, it's better to put the full path of node into the bash script (as I found out), like this: /usr/local/bin/node – IgorGanapolsky Aug 25 '11 at 17:03
  • And when I check /var/spool/mail/root, then it shows an error: – IgorGanapolsky Aug 26 '11 at 03:03
  • Rob Raisch, here is the error: ReferenceError: bin is not defined at Object. (/home/campaigns/reporting/scheduler/scheduler.sh:2:1) And here is my shell script: #!/bin/env sh /usr/local/bin/node /home/campaigns/reporting/scheduler/updateRedeemedBitlys.js – IgorGanapolsky Aug 26 '11 at 13:19
  • @Igor G.: `ReferenceError` is javascript error, check your javascript code or post it. – Daniel Aug 26 '11 at 13:21
  • You are right Dani. I had the node command in the crontab entry. When I removed that, it worked. – IgorGanapolsky Aug 26 '11 at 18:35
8

I'm using nvm install node.js in Linux, and seems the crontab don't know the path well, so I wrote the full path of node to execute the node file.

* * * * * /home/www/.nvm/versions/node/v8.11.1/bin/node /home/www/ss-config/index.js
afacat
  • 1,880
  • 1
  • 10
  • 8
  • 3
    which is fine until you use nvm to change node versions (i.e. use nvm exactly for the reason you installed it in the first place!), then this will break and given its in cron you might not notice its broken for a long long time!. Having said this, I don't know what the solution is, it seems there isn't a good one as cron doesn't look at the user's environment shell apparently, so you can't refer to nvm's env vars. I'm still researching for an answer.. – Andy Lorenz Jan 08 '20 at 11:43
5

It's explained pretty well over here.

This can happen if your binary (node in this case) is under /usr/local/bin. By default, crontab looks for the binaries under /usr/bin or /bin.

Vaibhaw K
  • 169
  • 2
  • 5
1

Making soft links like this might be a solution for using node in crontab when it was installed with NVM:

$ sudo ln -s "$NVM_DIR/versions/node/$(nvm version)/bin/node" "/usr/local/bin/node"
$ sudo ln -s "$NVM_DIR/versions/node/$(nvm version)/bin/npm" "/usr/local/bin/npm"
maxxx
  • 657
  • 7
  • 5
0

Try to actually run the command from the promp > then hit the endpoint and look at the catch message.

For me step 1

/usr/bin/node /var/www/iiiiii.net/server.js

then hit the endpoint (or url) with browser or postman.

I had the following error because my config did not pick up .env credentials when being run off of crontab.

 Access denied for user ''@'localhost' (using password: NO)

It could be any number of errors - but if you test it in this way it will reveal your particular error.

Michael Nelles
  • 5,426
  • 8
  • 41
  • 57