1

I have a cron pattern 0 0 0/6 ? * * for every six hour. i have used Nodejs cron for executing cron job, below is my code:

var CronJob = require('cron').CronJob;
  new CronJob('0 0 0/6 ? * *', function() {
          console.log(new Date(), 'Every 6 hours');
          try {
              // task to be executed
          } catch (e) {
              console.log(e);
          }
      }, function() {},
      true
  ).start();

I got this exception during startup of my program:

"stack": [
    "Error: Field (?) cannot be parsed",
    "    at Object._parseField (/home/ajit/git/test/BackEnd/node_modules/cron/lib/cron.js:344:11)",
    "    at Object._parse (/home/ajit/git/test/BackEnd/node_modules/cron/lib/cron.js:308:9)",

I have checked this pattern at this website: http://www.cronmaker.com/ , it says my cron is valid . I have searched over internet and i am unable to get a valid 6 digit cron pattern without ?.

Ajit Soman
  • 3,926
  • 3
  • 22
  • 41
  • My English knowledge says that: `? symbol/mark cannot be parsed, and I seet it first time` (: so fix is: `0 0 */6 * * *` btw cronmaker can understand crons with minutes not with seconds, so it may say that it's ok (: – num8er Aug 15 '17 at 14:09
  • 3
    Possible duplicate of [node-cron run job every 3 hours](https://stackoverflow.com/questions/41597538/node-cron-run-job-every-3-hours) – Himanshu sharma Aug 15 '17 at 14:10
  • I have tried this pattern `0 0 */6 * * *` it is not working . – Ajit Soman Aug 15 '17 at 14:12
  • @AjitSoman how do You measure it? maybe Your code has issues? – num8er Aug 15 '17 at 14:13
  • Right now i have set my system time to `11:59 PM` , cron job does not trigger – Ajit Soman Aug 15 '17 at 14:14
  • of, course, You've set system type, but have You restarted Your app? – num8er Aug 15 '17 at 14:14
  • This cron job is for 1 min `0 */1 * * * *`, it execute correctly – Ajit Soman Aug 15 '17 at 14:14
  • 1
    also why not use simple `crontab` ? – num8er Aug 15 '17 at 14:15
  • try to log `console.log(new Date())` on every tick of cron, seems like cron job has different timezone – num8er Aug 15 '17 at 14:16
  • I got this for every one minutes : `2017-08-17T18:33:48.783Z` – Ajit Soman Aug 15 '17 at 14:20
  • @num8er , i have used your cron pattern 0 0 */6 * * * in our production server and based on logs i found that the cron is executing only one time at 12:00 AM . i am expecting this scheduler to run at 12:00 AM, 6:00 AM, 12:00 PM and 6:00 PM – Ajit Soman Aug 18 '17 at 05:14
  • @AjitSoman off course it will not work. Linux crontab does not have seconds. So it has read first 5 elements (0 0 */6 * *) and last star was implemented as command line thing. So fix Your crontab: 0 */6 * * * – num8er Aug 18 '17 at 07:30

1 Answers1

1

As mentioned in the Cron package documentation, patterns are based upon this specification. You can't use a ? in the pattern.

Just replace the ? by a *, same as the 0 before the /6:

var CronJob = require('cron').CronJob;
  new CronJob('0 0 */6 * * *', function() {
          console.log(new Date(), 'Every 6 hours');
          try {
              // task to be executed
          } catch (e) {
              console.log(e);
          }
      }, function() {},
      true
  );

EDIT

Based on the documentation once again, it seems that the true flag passed as 3rd argument starts the job immediately so you don't have to call the start method.

Erazihel
  • 7,295
  • 6
  • 30
  • 53
  • i have set my system time to 11:59 PM but cron job does not trigger – Ajit Soman Aug 15 '17 at 14:15
  • i have checked your pattern at http://www.cronmaker.com/ it says invalid – Ajit Soman Aug 15 '17 at 14:24
  • Don't use cronmaker.com but https://github.com/kelektiv/node-cron#how-to-check-if-a-cron-pattern-is-valid – Erazihel Aug 15 '17 at 14:27
  • i have used your cron pattern `0 0 */6 * * *` in our production server and based on logs i found that the cron is executing only one time at 12:00 AM . i am expecting this scheduler to run at 12:00 AM, 6:00 AM, 12:00 PM and 6:00 PM – Ajit Soman Aug 18 '17 at 04:31
  • @AjitSoman off course it will not work. Linux crontab does not have seconds. So it has read first 5 elements (0 0 */6 * *) and last star was implemented as command line thing. So fix Your crontab: 0 */6 * * * – num8er Aug 18 '17 at 22:21