0

I have a crontab with different time to execute some task, for example every minute, every 10 min, 1 hour, daily... And i have a question, when some of this cron coincide in the same time for example, when 10 minutes execute, also execute 1 min cron and this cron execute in parallel... but I want to execute in sequence, for example all jobs in 1 minute, and the all jobs in 10 min... how can I do this??

1 Answer 1

0

With cron itself, I don't think you really can. I would probably script my way out of that: make a single script executed from cron every minute, and then run the tasks with separate intervals from that script. Something like this:

Crontab entry:

* * * * * /path/to/main_script.sh

And main_script.sh:

#!/bin/sh
mins=$[ $(date +%s) / 60 ]           # current time, rounded to minutes
run_1min_task.sh
if [ $[ $mins % 10 ] -eq 0 ] ; then  # mins divisible by 10 ?
    run_10min_task.sh                # run the every 10 min task
fi
if [ $[ $mins % 60 ] -eq 0 ] ; then  # same for 1 hour
    run_1hour_task.sh
fi 
if [ $[ $mins % 1440 ] -eq 0 ] ; then # 1440 = 24*60
    run_daily_task.sh
fi

You need to take into account the time zone if you care what hour the once-a-day task runs, the above should run it at 00:00 UTC. Compare to some other value than zero to change it.

Also, note that if your tasks can take more than 1 minute, you need to make sure they can cope with running simultaneously, or prevent them from doing that.

1
  • It's same works fine, thx
    – sdelcueto
    Commented Oct 13, 2016 at 13:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .