Geekzone: technology news, blogs, forums
Guest
Welcome Guest.
You haven't logged in yet. If you don't have an account you can register now.


nate

6473 posts

Uber Geek
+1 received by user: 458

Retired Mod
Trusted
Lifetime subscriber

#17670 6-Dec-2007 15:20
Send private message

I've got a PHP script that runs on the first day of every month, and I'd like it to run at a random time of day between 9-5pm (at the moment it fires at 9am every time). What's the simplest way to do this (using PHP + cron jobs + MySQL)?

I thought of using a script I found through Google for setting a script timeout but this never worked.

Create new topic
NokiaRocks
364 posts

Ultimate Geek
+1 received by user: 1

Trusted

  #98587 6-Dec-2007 16:41
Send private message

The only way I think you could do it would be to run a script that seeded a random time, and placed it in a text file or database, and then call the script every minute and run an if statement asking

Maybe someone else could think of a less resource hungry way to do it?



nate

6473 posts

Uber Geek
+1 received by user: 458

Retired Mod
Trusted
Lifetime subscriber

  #98590 6-Dec-2007 16:47
Send private message

NokiaRocks: The only way I think you could do it would be to run a script that seeded a random time time, and placed it in a text file or database, and then call the script every minute and run an if statement asking

Maybe someone else could think of a less resource hungry way to do it?


That's the solution I came up with as well, but thought it wasn't the best for the same reasons as you.

Pity cron doesn't have a random option.

foobar
186 posts

Master Geek


  #98688 7-Dec-2007 06:09
Send private message

If you trust the longevity of your processes (and uptime of your computer) you can just start a script at 9 am, which sleeps for a random amount of time before doing what you need it to do... ?




The blog: foobar on computers, software and the rest of the world: http://www.geekzone.co.nz/foobar



sleemanj
1514 posts

Uber Geek
+1 received by user: 315


  #98916 8-Dec-2007 17:43
Send private message

Assuming PHP is running as the same user owning the cronjob in question, then just add/remove the cron job daily (by the job itself) -

  get the current crontab with
  $Crontab = `crontab -l`

  then edit however you wish and set with
  $h = popen('crontab -', 'w');
  fwrite($h,$Crontab,strlen($Crontab));
  pclose($h);





---
James Sleeman
I sell lots of stuff for electronic enthusiasts...


chemuduguntar
5 posts

Wannabe Geek


  #101750 25-Dec-2007 12:05
Send private message

May I ask why it needs to be random ? ... if you wanted it to be truely random you could use a quantum random number generator ;)

domainspace
8 posts

Wannabe Geek


  #105466 17-Jan-2008 23:47
Send private message

NokiaRocks: The only way I think you could do it would be to run a script that seeded a random time, and placed it in a text file or database, and then call the script every minute and run an if statement asking

Maybe someone else could think of a less resource hungry way to do it?



Maybe you can go for quasi-random..

Just generate a random number between 0 and 23 and store that in the database then have your cron job run the script hourly. Running hourly and doing just one SQL query 23 times out of 24 shouldn't put much strain on the resources.

Create new topic








Geekzone Live »

Try automatic live updates from Geekzone directly in your browser, without refreshing the page, with Geekzone Live now.



Are you subscribed to our RSS feed? You can download the latest headlines and summaries from our stories directly to your computer or smartphone by using a feed reader.