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.




6335 posts

Uber Geek
+1 received by user: 395

Moderator
Trusted
Lifetime subscriber

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

  Reply # 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?



6335 posts

Uber Geek
+1 received by user: 395

Moderator
Trusted
Lifetime subscriber

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


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

1262 posts

Uber Geek
+1 received by user: 166


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


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


  Reply # 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 »

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 »

Orcon announces new always-on internet service for Small Business
Posted 18-Apr-2019 10:19


Spark Sport prices for Rugby World Cup 2019 announced
Posted 16-Apr-2019 07:58


2degrees launches new unlimited mobile plan
Posted 15-Apr-2019 09:35


Redgate brings together major industry speakers for SQL in the City Summits
Posted 13-Apr-2019 12:35


Exported honey authenticated on Blockchain
Posted 10-Apr-2019 21:19


HPE and Nutanix partner to deliver hybrid cloud as a service
Posted 10-Apr-2019 21:12


Southern Cross and ASN sign contract for Southern Cross NEXT
Posted 10-Apr-2019 21:09


Data security top New Zealand consumer priority when choosing a bank
Posted 10-Apr-2019 21:07


Samsung announces first 8K screens to hit New Zealand
Posted 10-Apr-2019 21:03


New cyber-protection and insurance product for businesses launched in APAC
Posted 10-Apr-2019 20:59


Kiwis ensure streaming is never interrupted by opting for uncapped broadband plans
Posted 7-Apr-2019 09:05


DHL Express introduces new MyDHL+ online portal to make shipping easier
Posted 7-Apr-2019 08:51


RackWare hybrid cloud platform removes barriers to enterprise cloud adoption
Posted 7-Apr-2019 08:50


Top partner named at MYOB High Achievers Awards
Posted 7-Apr-2019 08:48


Great ideas start in Gisborne with hackathon event back for another round
Posted 7-Apr-2019 08:42



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.