Cron schedule a task alleatory in a range of hours - random

I want schedule with cron a task to be ran ONCE in between a certain tange of hours, not every hour in a range.
Example:
I want it may run ONCE between 9 to 12 hours, single time.
Better if I can run the task ONCE between 9 to 12 hours and random minute as well. I don't want can be predictable, but I want keep control about in which range of hours it will be fired.
I tried:
# m h dom mon dow command
00-59 09-12 * * 2 /home/osmc/play.sh
But it ran every minute, between 00 and 59, for every hour between 9 to 12 (not desired)
Any idea how I can do so?

If you like to stick with cron, you could do the following:
0 9 * * 2 sleep $(( RANDOM \% 10800)); /home/osmc/play.sh
The job will start at 9AM, and delays the script for upto 10800 seconds, which is 3 hours.
Another solution found here:
Cron jobs and random times, within given hours

Related

How do i execute part of a shell script every 5 minutes and anothe rpart every 1 hour?

I have a shell script that collects some data and send it to destination. Part of the data should be copied every 5 minutes and other every 20 minutes. How can this be achieved in a single script? As of now i'm collecting the data every 5 minutes by scheduling with cron.
Best practice would be to use to separate files with two different cron entries. If you need to reutilize part of your code consider using functions.
If you still want to do it in only one file, you should run it every 5 minutes and on each run check whether or not you should execute the other part (every 20 min) or not.
modulo=$((`date +%_M)` % 20))
//do whatever has to be done every 5min
[...]
//check for modulo of current minute / 20
if [ $modulo -eq 0 ]; then
echo Current minute is `date +%_M)`, must execute part 2
//whatever has to be done every 20min
else
//do nothing
fi;
The reason why the variable modulo is defined in the first line is because what has to be done every 5min can potentially take longer than 1min to execute so by the time it is done minute is no longer 20 but 21. Defining the variable before is a safeguard to overcome this.

Magento 1.7 - Cron.php: too late for the schedule

I created some modules to be executed by magento cron but i get always the error.
The numbers:
Cron.php gets executed every 5 minutes
system/cron/schedule_generate_every = 15
system/cron/schedule_ahead_for = 30
system/cron/schedule_lifetime = 15
The module cronjobs should be executed every 5 minutes.
They are added correct to cron_schedule to be executed i.e. at 2014-01-16 16:40:00, 2014-01-16 16:45:00, 2014-01-16 16:50:00 ...
But on execution in 16:50 i get lots of errors. exception 'Mage_Core_Exception' with message 'Too late for the schedule.' also for jobs in the future.
Perhaps: our local time is 17:50, server time 16:50. But i can't remember we had this issue before on other cronjobs.
If the cronjob runs every 5 minutes, try this configuration:
Generate Schedules Every 5
(enter here the cronjob execution time, in this case 5 minutes)
Schedule Ahead for 125
(based on cronjob execution time plus the maximum time one job needs. For example: sitemap generation takes 120 minutes, then enter 120 minutes + 5 = 125 minutes)
Missed if Not Run Within 180
(runtime of the longest process, for example: an import takes 120 minutes, then enter 120 minutes + 60 minutes - because sometimes there is a difference between mysql and server time)
History Cleanup Every 10
(minimum cronjob execution time = 5 * 2 = 10 minutes in this case)
Success History Lifetime 1440
(duration of cronjob storage, to proof if everything works fine. 1440 = 24 hours)
Failure History Lifetime 1440
(duration of cronjob storage, to proof if there is an error. 1440 = 24 hours)
And last but not least, install AOE-Scheduler for a visual inspection of your cronjobs.
http://www.magentocommerce.com/magento-connect/aoe-scheduler.html

CRON: Run job on particular hours

I have a spring batch application and i am using CRON to set how often this application runs. But the problem i am running into is that i want to run the job on specific hours
3 am
7 am
11 am
3 pm
7 pm
11 pm
As you can see it is every 4 hours but starts at 3 am so i cannot use */4 in the hours section of the timing format as this would start the job at 4am
I have also tried '3,7,11,15,19,23' in the hours section but this does not work either (guessing it only works in the minutes section). Does someone know how i can do this?
Use
#Scedule(cron="0 0 3/4 * * ?")
The Pattern x/y means: where <timepart> mod y = x
or
#Scedule(cron="0 0 3,7,11,15,19,21 * * ?")
According to the Quartz Cron Trigger Tutorial:
The '/' character can be used to specify increments to values. For
example, if you put '0/15' in the Minutes field, it means 'every 15th
minute of the hour, starting at minute zero'. If you used '3/20' in
the Minutes field, it would mean 'every 20th minute of the hour,
starting at minute three' - or in other words it is the same as
specifying '3,23,43' in the Minutes field. Note the subtlety that
"/35" does *not mean "every 35 minutes" - it mean "every 35th minute
of the hour, starting at minute zero" - or in other words the same as
specifying '0,35'.
0 0 3,7,11,15,19,23 * * ?
Fires for 0 minute starting at 3am and ending at 23:00 pm every day.
judging by the two answers above the error i was making was i was keeping the apostrophe at the start and end of my hours... very silly
i managed to solve this by using 3-23/4 for the hour as this starts from 3am and then every other fourth hour (just a different way of doing it to the other answers)

VBS login script using between time ranges

I need to run a vbs script at login that will run a batch script based on a time range of between 22:00 and 06:00 the following day.
I have the current script as follows
If Hour(Now()) >= 20 AND hour(Now()) < 6 Then
//RUN SCRIPT
Else
//RUN OTHER SCRIPT
End If
Now the script runs fine when I use pre noon times e.g 6 and 11 but the about it does not. I can see the issue in that is is not factoring in the following days time and actually going back in time. What I need is the following
if time is 20:00 on day 1 but less than 06:00 on day 2 run the script else run the other script
This needs to run continuously between these times for every day of the week.
Please can you help?
Why not just change AND to OR? In that case when the hour of the day is greater than 20 then it will fire. if the hour of the day is less than 6 it will also fire. Look at it less of the time frame when it needs to fire and more of a time frame excluding those hours when it does not need to fire.
If Hour(Now()) >= 20 OR hour(Now()) < 6 Then
//RUN SCRIPT
Else
//RUN OTHER SCRIPT
End If
VB Script Date Functions

bash : Make job every x days

I have a bash script runned every day via cron.
In this bash I want to run some command but only every x day (x is fixed and will be 3, 4 or 5 I don't know at the moment)
I can test if the remainder of the division day of month / x equal 0
day_of_month % x = 0
I will work on a single month but not always between to month, for example months april - may with x = 3
1 2 3 4 5 6 7 8 9 ... 27 28 29 30 31 1 2 3 4 5 6 7 8 9 ... 27 28 29 30
x x x ... x x x x x ... x x
It not a big deal, but is there any other way to do this ?
PS : I don't want to explode my script and make 2 cron jobs (one every day : *, the other evevery x day : */x)
One easy solution would be to use "day of year" instead of day of month. Then there'd be no problem with different month lengths. You'd still get problems at the end of year, of course.
The "day of year" is available via date +%j.
If that is still not acceptable, you can use the seconds since 1970 (date +%s), and divide by days*24*60*60, then use the remainder of that. You'd still get problems with leap seconds, but otherwise it should be correct.
Using cron sounds the like the proper idea actually, because that is what cron is made for.
One idea though to solve your 'across months' problems: don't think in months, instead check whether the time-in-seconds-since-epoch % x*seconds-per-day is 0 ...
I would export a system variable with last date of execution and calculate the interval from it. It wouldn't work after a restart but I assume in this case the interval should be restored.
Have a look at the at command
Using that you can tell a script to call itself at a later date.
From manpage:
For example, to run a job at 4pm three
days from now, you would do at
4pm + 3 days, to run a job at 10:00am on July 31, you would do at
10am
Jul 31 and to run a job at 1am tomorrow, you would do at 1am
tomorrow.
If I had this problem, I would tell cron to run my script once every day, and inside the script I would calculate whether or not work needs to be done. That way, you have full control over it.
For example,
dayno=`date +%d`
let divided=daynum%3
if [ $divided == 0 ] ; then
exit 0
fi
# real work begins here
This script would work on any system that uses bash.
Does your job need to run every day? That is, does it do something on those days when your calculation does not result in a 0. If not, you could set up a job in cron thus:
0 12 3,6,9,12,15,18,21,24,27,30 * * <your job>
and it would run every third day (at 12 noon in my example).
I'm pretty sure that "Feb 30" would be safely ignored.

Resources