![]() |
Random Cron Job Possible? or alternative?
i want to execute a script once per day randomly in a 6 hour period e.g. execute a script between 5am and 11am every day. obviously if i'm using a cronjob it's got to be a regular time every day, anyone got a suggestion of how i can execute it randomly?
thanks in advance |
My idea of doing this would be to run the cronjob multiple time over the 6 hour span and have a random variable inside the script to make it execute or not. You will also need a value stored somewhere to see if you've already executed the script within the last 6 hours.
Good luck! |
Quote:
Generate a random number between 1-10, then if the random number is < or = a variable then execute the script. Otherwise exit with no action taken. If you set the variable to 2 for instance your script would have a 20% chance of executing. |
Actually, that still leaves the chance that your script will never, ever execute.
Try just cronning your script once and having your script sleep for a randomized number of seconds. That way, it will run, you just won't be certain when :) (By the way, shells by default don't have a random number generator as far as I know. You might have to find yourself a random number "program" for that) |
Oh, actually. It seems that bash has something of a $RANDOM enviroment variable which is .. probably pseudo-random enough for what you want.
|
Quote:
Generate a random number between 1-6. Store that along with the date in a flat file. When the script runs, have it choose a number between 1-6. If that = the random number then execute the script. You'd have to keep track of the numbers already picked for the day as well as increment the date after the script has already run for the day. Make sure the script doesn't execute again if the date in the text file == today. |
thanks guys for all the responses, there's some good sugestions there, i'll go away and have a play with it and see if i can come up with something using your suggestions. i'll post back if i come up with something usable. too be honest i assumed there would already be a utility or something that did this already.
thanks again |
Quote:
|
Quote:
i'm just having a quick search on google and found the php sleep() function (its a php script i'm trying to run). which maybe a solution but has anyone used this before and if so can i use this for a period of hours (2 hours would do)? and would this function be affected by my php.ini settings e.g. max_exectution_time etc? |
Quote:
why 1-6? |
Quote:
5am 6am 7am 8am 9am 10am 11am |
just make a wrapper in perl or something
|
A simpler approach:
1)Have 1 daily cron job run a script that generates a random time tag for the day and store it in a db or text file 2)have a second cron running once ever 30 minutes or whatever and check to see if it has passed the randomly generated time. If it has, fire off the task and reset the stored time so it only does it once. |
Quote:
|
Why not just use cron to do a daily 'at'? I fail to see the need for abusing cron for two jobs that cron, and at, would do better.
|
run the cron job at a set time, but have the code that it runs, php or other hand the randomization. A simple example would be to have it generate a random number between 1 and 10 and if the number if 7 then run execute the rest of the job, if not exit.... then it'll try it again next time the cron job runs. You'd be bound to have it be 7 now and then.
|
Quote:
i've never heard of 'at' before with regards to cron, will that achieve some sort of randomization? |
| All times are GMT -7. The time now is 10:09 PM. |
Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2026, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123