-2

Is it possible to create a CRON job that can run every 130 secs or 2 min and 10 secs.

0

If you are on a systemd distro you can use a systemd timer to get this level of timing resolution. Depending on your use case, you can use it as a delta-t (eg every x seconds since boot) or configure it using wall time ( eg at 00:00:10, 00:02:10, etc.).

Briefly, you would create 2 files.

  1. The first is the .timer file, described here. This will set up the timing of the timer events and configure what happens when the timing event occurs. There are a number of criteria to define - for example - do you want it to run immediately at boot and then every 130 seconds? Do you want it to run if it you are at a lower run level? Do you want it to continue to run if it encountered errors? Systemd gets, arguably, complex far quicker than the old approachs with init files and cron jobs. However, using it instead of cron will resolve your timing issue.

  2. The second file is typically a .service file that gets called by the timer service. In this file you would define what the cron job does - either a simple command or call another script to allow more sophisticated tasks. This service file would normally be setup as a 'oneshot'. See here for more details.

The files should be copied into /etc/systemd/system/ and you will run the following to enable / start the timer service:

 systemctl enable mycronjob.timer
 systemctl start mycronjob.timer

You can use journalctl to review / debug your implementation. For far more information, see the archwiki discussion.

1

The finest granularity that cron provides is 1 minute. I guess you could run your job every 1 minute and have it determine if it needs to run before the next minute, wait the correct amount of time and then proceed.

  • Calculate the number of seconds since midnight modulo 130.
  • Subtract this number from 130
  • If the answer is less than 60 wait that number of seconds and proceed.
  • If the answer is 130 proceed immediately
  • No, work starts every 120 seconds: at 00:00:10, 00:02:10, 00:04:10 etc. – Stephen Kitt Sep 5 '16 at 15:45
  • Wanders off to get more coffee – Iain Sep 5 '16 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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