GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   Random Cron Job Possible? (https://gfy.com/showthread.php?t=840217)

roly 07-09-2008 08:36 AM

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

xenilk 07-09-2008 08:40 AM

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!

Babaganoosh 07-09-2008 08:55 AM

Quote:

Originally Posted by xenilk (Post 14440140)
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!

Exactly.

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.

Dido 07-09-2008 09:11 AM

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)

Dido 07-09-2008 09:13 AM

Oh, actually. It seems that bash has something of a $RANDOM enviroment variable which is .. probably pseudo-random enough for what you want.

Babaganoosh 07-09-2008 09:19 AM

Quote:

Originally Posted by Dido AskJolene (Post 14440232)
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)

That's kinda true. I didn't really read his post. There's still a simpler way to do this.

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.

roly 07-09-2008 09:37 AM

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

Babaganoosh 07-09-2008 09:38 AM

Quote:

Originally Posted by roly (Post 14440317)
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

There may be something out there already. I'm a programmer by trade so it's in my nature to over-complicate everything.

roly 07-09-2008 09:48 AM

Quote:

Originally Posted by Babaganoosh (Post 14440321)
There may be something out there already. I'm a programmer by trade so it's in my nature to over-complicate everything.

i'm not a programmer as you can probably tell :)

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?

d-null 07-09-2008 10:41 AM

Quote:

Originally Posted by Babaganoosh (Post 14440262)
That's kinda true. I didn't really read his post. There's still a simpler way to do this.

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.


why 1-6?

ADL Colin 07-09-2008 10:46 AM

Quote:

Originally Posted by jetjet (Post 14440579)
why 1-6?

6 hours. You could use 7 and include both end points.

5am 6am 7am 8am 9am 10am 11am

buyandsell 07-09-2008 10:47 AM

just make a wrapper in perl or something

gornyhuy 07-09-2008 10:51 AM

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.

roly 07-09-2008 12:24 PM

Quote:

Originally Posted by gornyhuy (Post 14440636)
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.

this looks like the way i'll make it work, thanks :)

GrouchyAdmin 07-09-2008 12:38 PM

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.

teg0 07-09-2008 01:48 PM

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.

roly 07-10-2008 12:13 PM

Quote:

Originally Posted by GrouchyAdmin (Post 14441019)
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.


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