Cron - run job every minute but first execution is 5 minutes after boot

So my problem is the following, I need to run a job (lets call it job-A) every minute, but before it can be executed I need to start a server and do other things. I mean, once all those executions are done I need to run job-A every minute.

My crontab looks like this:

@reboot first-required-job.sh
@reboot second-required-job.sh
@reboot third-required-job.sh

My idea is to let the first execution of job-A start 5 minutes after boot, to be sure that all the previous processes are done, and then run every minute.

If I do something like

@reboot (sleep 300; job-A.sh)

it will only run once after boot.

There must be a simple way to do this, but I can't figure it out. How can I do this??

0

1 Answer

There must be a simple way to do this

In my point of view, your sleep 300 solution is already a simple way. It's not clean, because the other jobs might take longer than 300 seconds. But it's a simple way.

My idea for a clean solution is to let the other scripts create a file once they are done. This way the job-A is able to find out if the precondition is met, just by checking the existence of that file.

I would wrap the other 3 scripts into a new script, eg. job-A-preconditions.sh. That Script would look like this:

#!/bin/bash
/path/first-required-job.sh
/path/second-required-job.sh
/path/third-required-job.sh
touch "/tmp/very-unique-filename-to-signal-job-A-can-start"

The crontab would look like this:

# run the preconditions job only once after boot
@reboot /path/job-A-preconditions.sh
# run job-A every minute
* * * * * test -f "/tmp/very-unique-filename-to-signal-job-A-can-start" && /path/job-A.sh

Note: It's important that the signal file gets deleted automatically, before the precondition jobs start. This happens automatically if your /tmp/ uses tmpfs (aka ramdrive). If you have systemd-tmpfiles-clean.service enabled, /tmp/ gets cleaned before shutdown, so this works too.

Another note: The deluxe version would be to use systemd for all jobs involved, and just use the depency mechanism of systemd.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like