How to set cron job correct path to run a node.js script? - node.js

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?

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.

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
});
For more detail, please refer this example.

Related

Running node script with crontab

I'm trying to run a node script with crontab. I've tried first doing things like
* * * * * echo test > test.txt
to be sure crontab works (I'm trying to make the command work and then I'll change the crontab to something different so it doesn't run every minute).
The crontab above works. The thing is, when I try to use node, it doesn't run with the crontab. Running which node I get /usr/bin/node Here are the things I've tried.
Thanks!
* * * * * cd /path/to/script && node script.js
* * * * * cd /path/to/project && npm start (runs npx tsc && node build/script.js)
* * * * * cd /path/to/script && node script.js > test.txt (file is generated empty, even though, script has console.log)
* * * * * node /path/to/script/script.js
* * * * * echo test > test.txt && node /path/to/script/script.js (file gets generated)
Also I've tried all of the above replacing node by /usr/bin/node.
If I run any of these commands manually, it executes the program.
After going over a long time and testing a lot of stuff, I realized the issue was doing sudo crontab -e to set the crontab. I fixed it after running instead
sudo crontab -u username -e

CRON job outputting blank lines

I tried to create a CRON job that runs every minute. As a root user, I ran crontab -e, and in the cronjob, I put in my command * * * * * /usr/bin/php {redacted}/index.php > {redacted}/output.txt
the {redacted} file path has permissions added: chmod ogu+rwx -R so everyone should be able to access it. I even created a new user with no sudo or root privileges, and running /usr/bin/php {redacted}/index.php > {redacted}/output.txt obviously writes the output of {redacted}/index.php to {redacted}/output.txt. However, my cron job ends up overwriting and turning the .txt file to just blank, nothing. I have no idea what is going on since I already made sure there were no permission errors, and cron jobs don't seem to have a visible log or output?
I would love to have any ideas about this. I even tried to add the cron job * * * * * root /usr/bin/php {redacted}/index.php > {redacted}/output.txt, all to the same output.
One more thing... I have tried to set the {redacted} location to /var/www/html, /var/www/{a user with sudo perms}, and {/usr/local/bin} all with the same result.
Update:
I tried something as simple as * * * * * php -v > {redacted}/output.txt and * * * * * /usr/bin/php -v > {redacted}/output.txt still with the same result, so it seems like the error is not with requiring root permissions to run my php program

Run yarn script into crontab

I made a script in TypeScript that download data from some api and store inside a mongo DB.
If i run yarn start from the app folder it works well.
I would like to put this command in a cron job that will be executed every 5 minutes.
I try it with some sintax in crontab but ti doesn't work.
I try to put the call in a run.sh script but it doesn't work too.
*/5 * * * * cd /opt/app-folder/src/ && /home/username/.nvm/versions/node/v16.15.1/bin/ts-node main.ts
*/5 * * * * cd /opt/app-folder && /usr/bin/yarn start > /home/username/app-name-out.txt
*/5 * * * * /home/username/run.sh > /home/username/app-name-out.txt
*/5 * * * * /home/username/.nvm/versions/node/v16.15.1/bin/ts-node /opt/app-folder/src/main.ts > /home/username/app-name-out.txt
*/5 * * * * cd /opt/app-folder/src/ && /home/username/.nvm/versions/node/v16.15.1/bin/ts-node main.ts > /home/username/app-name-out.txt
Can someone help me to execute the main.ts every 5 minutes?
Thanks
I get rid of this problem.
There was 2 problems, the first related to the output redirection.
I fixed by redirect stdout in a file and stderr in another one.
The second was related the the $PATH of crontab: it was /usr/bin:/bin.
To fix it I log into my user where script works and I print my $PATH with echo $PATH.
I copied the value and I set it before the crontab line in crontab file.
This is what it looks like:
# Set the same path of user username to have the correct path in script
PATH=/home/username/.nvm/versions/node/v16.15.1/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/snap/bin
# Execute oracle every 5 minutes
*/5 * * * * /bin/sh /home/username/run.sh >> /home/username/app-name-info.txt 2>> /home/username/app-name-error.txt
Now it works.

Cron job not getting triggered on Ubuntu

I am trying to schedule a job using cron.
Following are the steps that i did:
sudo vi /etc/crontab
Added the following line :
* * * * * root /bin/ls >> corn_example
Still job is not getting triggered.
I suspect it's because you're not specifying the folder to 'ls' or a folder location for the example file to be create in.
I'd try:
* * * * * root /bin/ls /tmp/ >> /tmp/corn_example

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

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?
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
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.
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.
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
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.
This also works for user-level cron jobs
*/2 * * * * cd /home/destination/path && $(which node) index.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
NVM users:
Save the following script (say, cronjob.env.sh) anywhere at your PATH (let's say: $HOME/bin). Remember to make it executable (chmod +x). Replace 'username' with your user name as well:
#!/bin/bash
export NVM_DIR="/home/username/.nvm" #replace "username" with your user name
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
Edit cronjob:
crontab -e
Insert/edit your job:
0 0 * * * (. ~/bin/cronjob.env.sh; ~/bin/my-script-with-node-cmd.sh)
Calling node command (instead of some shell script), like the below job, works as well:
*/1 * * * * (. ~/bin/cronjob.env.sh; node ~/index.js)
PM2 users:
A different approach is using pm2, with --cron option, which accepts cron patterns (inside quotation marks):
pm2 start index.js --no-autorestart --cron "0 0 * * *"
--no-autorestart is for one-time scripts (otherwise, it will be restarted every time it is completed).
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
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.

Resources