0

I have python and node scripts set as cron jobs on EC2. The python scripts run but the node scripts do not run. The node scripts run fine when activated manually from the command line. This is a simple script that finds out if today is a holiday and then puts the boolian answer into a file named isHoliday.csv.

What I've tried so far:

  1. Set as crontab using: crontab -e (last entry in this pic) enter image description here
  2. Set as crontab using: sudo crontab -e enter image description here
  3. Created a strange cron.d file, put the cron details in it, and chmod'd it 0644, using: sudo crontab -e; shown above. cron.d contents: enter image description here

I see the log file is named holiday_script.log in the sudo cron, and holiday.log in the cron.d file, but neither log file has been created.

  1. Replaced relative paths with absolute paths. (But I think this is still the most likely culprit.)

Code in the holiday.js file:

// Check if it is a holiday, t/f
var holidayChecker = require('usa-holidays');
var date = new Date();
var holidayObj = holidayChecker.make(date);
var isHoliday = !holidayObj.isBusinessDay();

// put the answer into a file so it can be read by python crons
var fs = require('fs');
fs.writeFile("/home/ec2-user/stack/isHoliday.csv", isHoliday, function(err) {
    if(err) {
        return console.log(err);
    }
});

enter image description here

2 Answers 2

1

I don't know the exact solution, but you can view your cron logs by moving to a different directory. When you first login to EC2, do this twice: cd .. That will take you up two directories. Then you'll need to switch user to sudo like this: sudo su Then move to the var/log folder. There should be a file named "cron". Since you have crons setup as *****, that file should be filling up with data every second. Tail it by typing: tail -f cron. That will allow you to see the crons execute in real time.

Also, the "mail" notice: navigate to that folder and use less to read the mail. That file will have more clues. Scroll down with the down-arrow.

That isn't the answer but it will help you move forward a bit.

1
  • Tailing the mail was the key. It said "node" was undefined. Changing the path to node made it work. The final cron is like this: * * * * * /home/ec2-user/stack/node/node /home/ec2-user/stack/holiday.js
    – Randy
    Jan 7, 2018 at 19:35
0

If you just want to run JS/TS code at a scheduled time interval without worrying about cron-jobs or servers, you can check out services like https://chunk.run

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.