1

Is it possible to run a cron every 86410 seconds or simply every 1 day and 10 seconds?

I have a service which takes 24 hours to process the data from the moment it is called! Now, I need to make sure that I am giving the service enough time to process the data so instead of calling the service every 24 hours, I need to call the service every 24 hours and few seconds!

Is it possible using a cron?

Vivek Kumar
  • 347
  • 3
  • 10
  • 1
    I don't think it's possible to have a time period less than a single minute in cron, but `1 */24 * * *` but will run a job every 24 hours + 1 minute. I would if possible, however, recommend running all your jobs sequentially i.e. using the ampersand operator, i.e. `0 */24 * * * process_info_script && collect_info_script`. That way the collection process won't start up again until your processing script has finished executing. – ZombieTfk Oct 26 '18 at 08:44

1 Answers1

0

I think it is interesting to note that if you do want to restart the processing every 86410 seconds, then your service start times would drift over days, later and later every time - so if you originally scheduled your process to start at 08:00, after about a year it would be starting at 09:00, and after about 23.6 years, it would go around the clock to start again at 8am.

Cron was definitely not designed for that kind of thing :-)

But if you are running on a recent Linux OS, you can use SystemD timer units to do exactly that. You may be familiar with SystemD service units - as this is how you write services for modern Linuxes, but SystemD can do a lot more, and one of those things is scheduling things that require interesting schedules.

Supposed you run your processing job as a SystemD service, it may look something like this:

/etc/systemd/system/data-processing.service

[Unit]
Description=Process some data
[Service]
Type=simple # its the default, but I thought I'd be explicit
ExecStart=/usr/bin/my-data-processor

You can then set up a timer unit to launch this service every 86410 seconds very simply - create a timer unit file in /etc/systemd/system/data-processing.timer with this content:

[Unit]
Description=start processing every day and 10 seconds
[Timer]
OnBootSec=0 # Start immediately after bootup
# Start the next processing 86410 seconds after the last start
OnUnitActive=86410
AccuracySec=1 # change from the default of 60, otherwise 
# the service might start 86460 after the last start
[Install]
WantedBy=timers.target

Then just enable and start the timer unit - but not the service. If the service is enabled, you probably want to disable it as well - the timer will take care of running it as needed.

systemctl daemon-reload
systemctl enable data-processing.timer
systemctl start data-processing.timer

Looking at it a bit more, you mentioned that you want to start the next run of the service after the previous run has completed. What happens if it doesn't take exactly 86400 seconds to finish processing? If we change the requirement to be "restart the data processing service after it finished running, but give it 10 seconds to cool down first" then you don't need a timer at all - you just need to have SystemD restart the service after a 10 seconds cooldown, whenever it is done.

We can change the service unit above to do exactly that:

[Unit]
Description=Process some data
[Service]
Type=simple
ExecStart=/usr/bin/my-data-processor
Restart=always
RestartSec=10
Guss
  • 24,799
  • 13
  • 87
  • 109