How can you execute a Node.js script via a cron job?
node.jsCronCrontabnode.js Problem Overview
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?
node.js Solutions
Solution 1 - node.js
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
Solution 2 - node.js
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.
Solution 3 - node.js
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.
Solution 4 - node.js
You can also specify paths to binary files on top of your user crontab like:
PATH=/bin:/usr/bin:/usr/local/bin
* * * * * cd your/path && node foo.js
* * * * * cd your/path && npm run bar
Solution 5 - node.js
in my laptop using Linux mint the given path not working so i used this to get a work around.
$ which node
$ /usr/bin/node
this worked for me.
Solution 6 - node.js
I don't know if changing your relative paths in your script to absolute paths is a good idea
(what happens when your file system changes or you deploy in another environment?)
You could try wrapping it in a shell script, setting some environment variables in the crontab execution. (specifically PATH
& NODE_PATH
for starters)
Try my suggestion for this similar question:
https://stackoverflow.com/a/27823675/608269
Solution 7 - node.js
This also works for user-level cron jobs
*/2 * * * * cd /home/destination/path && $(which node) index.js
Solution 8 - node.js
Use absolute paths for the node alias and the file to be run.
Edit Cron Jobs
crontab -e
Entry to Run Our Node File
This will run every minute.
*/1 * * * * * /bin/node /public/test.js
Full Tutorial https://askmacgyver.com/blog/tutorial/how-to-run-node-scripts-from-a-cron-job
Solution 9 - node.js
If you want to preserve the nvm
functionality and allow your code to get an updated node version without needing to change the cron job info, put your job in a shell script that first sets up nvm
, switches to proper node
version, then runs the actual job. Here is an example for a project called invoicing
that includes a cron
script in its package.json
.
File invoicing.sh
:
#!/bin/bash
cd /home/produser/invoicing
export NVM_DIR="/home/produser/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
npm run cron
Then set this up as the command in your cron job listing:
0 16 1 * * produser /home/produser/invoicing/invoicing.sh
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. Note that you must also make sure the server has the required node version.