How can you execute a Node.js script via a cron job?
Additionally, just put #!/usr/local/bin/node
at the top of the script you want to execute. Then it will automatically know to execute the script with node. Make sure the file is executable as well.
These answers here saying using absolute path will all cause major problems for running a larger node app!
Real Complete Solution
Edit Cron Jobs
crontab -e
Find Node Path
which node
CD into the destination folder, then Change Cron Job according to Node Path and run script
*/2 * * * * cd /home/destination/path && /bin/node index.js
This will then allow you to run a full NodeJS application without all the errors like how using an absolute path for your index.js file.
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