Running node script with crontab - node.js

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

Related

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.

Cronjob won't trigger .sh script

I have made this Cronjob that should run some tests. My script works, but cronjob won't trigger it.
Cronjob looks like this:
*/1 * * * * /bin/sh cd ~/Desktop/abc.sh
I want it to run every minute, just for testing purposes.
And my script is:
while read LINE; do curl -o /dev/null --silent --head --write-out "%{http_code} $LINE\n" "$LINE"; done < todo | tee test_results.txt
I can't even find the solution on google or youtube.
If you added #!/bin/bash to your script, then your cronjob should look like:
* * * * * ~/Desktop/abc.sh
Or
* * * * * /home/USER/Desktop/abc.sh
In the first case you have to run cronjob from the same user where of the Desktop folder.

Cronjobs do not run

I'm trying to run a cronjob to start and stop a server under a non-sudo user. I've tried asking others and doing what I saw from looking on google before asking here, but I'm still stuck.
Here's what's in my crontab for the server user:
* * * * * /home/server/startup/stop.sh
* * * * * /home/server/startup/start.sh
Here is what is in my stop.sh script:
#! /bin/sh
screen -r server -X quit
Everything runs normally if I run it using sh, and I only encounter a problem when using cron.
From what I see there could be 2 possible problems:
If the lines you are running in crontab are (and only those):
home/server/startup/stop.sh
home/server/startup/start.sh
then you are missing the time part of the line. If you want to run your program only once on boot you can run:
#reboot home/server/startup/start.sh
You are not giving the full path to your program (possibly you are just missing a / in the begging). Try running
* * * * * /home/server/startup/start.sh
or
#reboot /home/server/startup/start.sh
If these don't work I recommend you try the following to troubleshoot the issue:
Run the command using sh in the cron:
* * * * * /bin/sh /home/server/startup/start.sh
Try redirecting the stdout and stderr of your command to a file and see if any errors occur

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

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.

Pi Cron Job Fails to Run

Hoping someone can help with a crontab issue I am having on the pi.
I have a program, rf24_receiver.py, that resides in the /home/pi directory. When run from terminal, it requires sudo to run. So with this command on terminal,
sudo python3 rf24_receiver.py, the program executes perfectly.
However, I need to run it on a schedule, so I tried the following without success.
crontab -e
*/30 * * * * sudo python3 /home/pi/rf24_receiver.py >> /home/pi/rf24_receiver.py.log 2>&1
Then I tried this
*/30 * * * * python3 /home/pi/rf24_receiver.py >> /home/pi/rf24_receiver.py.log 2>&1
Then I tried this
sudo crontab -e
*/30 * * * * sudo python3 /home/pi/rf24_receiver.py >> /home/pi/rf24_receiver.py.log 2>&1
Then I tried this
*/30 * * * * python3 /home/pi/rf24_receiver.py >> /home/pi/rf24_receiver.py.log 2>&1
No success with any of the above. I hope this is the right place to post this.
Any help would be appreciated.
Thank you
Sorry to bother the forum with this question. I am not sure why I got this command to work under sudo crontab -e:
*/29 */1 * * * sudo python3 /home/pi/rf24_receiverR3.py >> /home/pi/rf24_receiverR3.py.log 2>&1
I did a reboot and it seemed to work after that.

Resources