10

I have a cron job setup, with the minimum value of 60 seconds, and I want the program to be able to run at second intervals i.e. whatever I set it as 60 seconds onwards.

So for example, I want the cron job to run every 65 seconds, or every 63 seconds, or every 160 seconds etc.

Is this possible? Or is cron job only set to run at 60 second intervals?

If yes, what would the cron job for this look like?

I managed to make it run every 'x' minutes: So far, I have managed to use a database value which the user inserts to run the cron job every set number of minutes. Which looks like this:

  cron.schedule('*/' + test + ' * * * *', function () {
    console.log('running a task every minute');
  });

With the schema looking like:

var CronSchema = new mongoose.Schema({
  minutes: { type: Number, min: 1 }
})

How can I run a cron job every 'x' seconds? Ideally, I would only allow a user to input a value in seconds, so if they want the job to run every two minutes they would have to input 120 seconds.

Markus W Mahlberg
  • 19,711
  • 6
  • 65
  • 89
deeveeABC
  • 960
  • 3
  • 12
  • 34
  • Are you actually using [tag:cron] or not? – OrangeDog Oct 17 '16 at 12:39
  • 1
    Why not use `setInterval`? – DrakaSAN Oct 17 '16 at 12:40
  • The Linux `cron` daemon only checks every minute. To run anything on a x second timer you would need to involve some other mechanism. – Klas Lindbäck Oct 17 '16 at 12:41
  • Yes I am actually using cron. The main aim is to allow the user to set how often the cron job will run. Could I do that with setInterval? And what other mechanism is needed? – deeveeABC Oct 17 '16 at 12:45
  • It looks like you're using cron middleware for Node, which generally just uses javascript timers under the hood. – adeneo Oct 17 '16 at 12:51
  • Anyway, as noted above, Linux Cron only runs every minute, however Cron middleware for Node can run every second, or whatever you set it to, the pattern used for most of these modules is the same as regular cron, with the addition of seconds. – adeneo Oct 17 '16 at 12:55
  • So how could I set this in order to allow the user to input the value in seconds? – deeveeABC Oct 17 '16 at 12:56

2 Answers2

16

If you're using middleware for Node, you can add seconds as well

cron.schedule('*/' + test + '0,30 * * * * *', function () {
    console.log('running a task every minute');
});

Will run every 30 seconds, i.e. on xx:00:000 and xx:30:000

Note that javascript versions have six values, and not five like native Linux Cron, meaning the sixth is for seconds, which would be the first one

*    *    *    *    *    *
┬    ┬    ┬    ┬    ┬    ┬
│    │    │    │    │    |
│    │    │    │    │    └ day of week (0 - 7) (0 or 7 is Sun)
│    │    │    │    └───── month (1 - 12)
│    │    │    └────────── day of month (1 - 31)
│    │    └─────────────── hour (0 - 23)
│    └──────────────────── minute (0 - 59)
└───────────────────────── second (0 - 59, OPTIONAL)

Full documentation on allowed values can be found in the crontab documentation

adeneo
  • 312,895
  • 29
  • 395
  • 388
  • What if I want to run it every 38 seconds? Its not possible and its only possible for multiples of 60. – coder Jul 19 '18 at 10:33
  • 1
    @coder, Why did you decide that it is impossible to run every 38 seconds ? If you use ```cron``` npm package you can run without problems. For example, I run a script every 15 seconds and I write the following: ```*/15 * * * * *```. Maybe, I don't know anything else. – Aleksei Korkoza Feb 08 '19 at 11:35
  • @AlexeyKorkoza, when I said multiples of 60 , I meant nos which can divide 60, eg, 1,2,3,4,5,6,10,12,15,20,30. You can do it for multiples mentioned but not for 38 seconds See this link for cron info https://crontab.guru/#*/38_*_*_*_* – coder Feb 08 '19 at 12:23
  • When they developed Cron for unix-based systems back in the day, they didn't account for seconds at all, mostly because it's usually not necessary to run a program on the server more often than anything defined in minutes. Most of the time you'll end up with crontabs doing work once every hour or day, not every 15 seconds. These days, things are a little different, sometimes it does make sense to poll something more often than every minute. Javascript based programs, like for Node, probably use setInterval, and can be set to any "second" value, or even milliseconds if allowed for. – adeneo Feb 15 '19 at 00:31
1
cron.schedule('*/' + test + '0,x * * * * *', function () {
   console.log('running a task every x seconds');
});

this will run every x seconds.

anant
  • 79
  • 2
  • 10