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.




6358 posts

Uber Geek

Moderator
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
362 posts

Ultimate Geek

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?



6358 posts

Uber Geek

Moderator
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.

 
 
 
 


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... ?

1287 posts

Uber Geek


  # 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...


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 ;)

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



Twitter and LinkedIn »



Follow us to receive Twitter updates when new discussions are posted in our forums:



Follow us to receive Twitter updates when news items and blogs are posted in our frontpage:



Follow us to receive Twitter updates when tech item prices are listed in our price comparison site:





News »

OPPO Reno2 Series lands, alongside hybrid noise-cancelling Wireless Headphones
Posted 24-Oct-2019 15:32


Waikato Data Scientists awarded $13 million from the Government
Posted 24-Oct-2019 15:27


D-Link launches Wave 2 Unified Access Points
Posted 24-Oct-2019 15:07


LG Electronics begins distributing the G8X THINQ
Posted 24-Oct-2019 10:58


Arlo unveils its first video doorbell
Posted 21-Oct-2019 08:27


New Zealand students shortlisted for James Dyson Award
Posted 21-Oct-2019 08:18


Norton LifeLock Launches Norton 360
Posted 21-Oct-2019 08:11


Microsoft New Zealand Partner Awards results
Posted 18-Oct-2019 10:18


Logitech introduces new Made for Google keyboard and mouse devices
Posted 16-Oct-2019 13:36


MATTR launches to accelerate decentralised identity
Posted 16-Oct-2019 10:28


Vodafone X-Squad powers up for customers
Posted 16-Oct-2019 08:15


D Link ANZ launches EXO Smart Mesh Wi Fi Routers with McAfee protection
Posted 15-Oct-2019 11:31


Major Japanese retailer partners with smart New Zealand technology IMAGR
Posted 14-Oct-2019 10:29


Ola pioneers one-time passcode feature to fight rideshare fraud
Posted 14-Oct-2019 10:24


Spark Sport new home of NZC matches from 2020
Posted 10-Oct-2019 09:59



Geekzone Live »

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


Support Geekzone »

Our community of supporters help make Geekzone possible. Click the button below to join them.

Support Geezone on PressPatron



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.

Alternatively, you can receive a daily email with Geekzone updates.