0

I'm using cpanel, and I want to make a cron job that executes a certain node.js script every hour. I set it up by selecting every hour, and in the command box I put cd node-bin && node check-up.js, but after an hour it didn't execute the nodejs script. What am I doing wrong? Do you have to specify where to put the results (using >)? Does it not run the command from the home directory? I'm on a shared hosting server, so I would assume it runs it from the home directory, but who knows...any ideas?

UPDATE:

I set it up to email me results, and it now says that it can't find the command node (even though I can run it from SSH). Why?

markasoftware
  • 12,292
  • 8
  • 41
  • 69
  • If node is on the binary search path in an interactive session, then `which node` will you give the absolute path of the executable, and this absolute path is what needs to be after the #! – Paul Jun 11 '14 at 06:27

2 Answers2

0
  • make sure there is an empty line at the end of your crontab
  • add * * * * * env > /tmp/env.txt and check which environment variable is missing because the crontab environment is usually different.
Fabricator
  • 12,722
  • 2
  • 27
  • 40
0

Here are the two essential steps for a cron job to run:

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

First, add

#!/usr/local/bin/node

to the beginning of your node.js file, and give run permission (

chmod 777 node.js

) (You can additionally check if it's ok, by typing

./node.js 

in the terminal) If this is ok, then you should specify the full path of the script in the cron job, and thats all! Also, as user3678068 pointed out, you should specify the full path of the output file! Hope this helps!

Community
  • 1
  • 1
sboda
  • 363
  • 4
  • 10