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

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

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

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



sleemanj
1490 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...


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





News and reviews »

Air New Zealand Starts AI adoption with OpenAI
Posted 24-Jul-2025 16:00


eero Pro 7 Review
Posted 23-Jul-2025 12:07


BeeStation Plus Review
Posted 21-Jul-2025 14:21


eero Unveils New Wi-Fi 7 Products in New Zealand
Posted 21-Jul-2025 00:01


WiZ Introduces HDMI Sync Box and other Light Devices
Posted 20-Jul-2025 17:32


RedShield Enhances DDoS and Bot Attack Protection
Posted 20-Jul-2025 17:26


Seagate Ships 30TB Drives
Posted 17-Jul-2025 11:24


Oclean AirPump A10 Water Flosser Review
Posted 13-Jul-2025 11:05


Samsung Galaxy Z Fold7: Raising the Bar for Smartphones
Posted 10-Jul-2025 02:01


Samsung Galaxy Z Flip7 Brings New Edge-To-Edge FlexWindow
Posted 10-Jul-2025 02:01


Epson Launches New AM-C550Z WorkForce Enterprise printer
Posted 9-Jul-2025 18:22


Samsung Releases Smart Monitor M9
Posted 9-Jul-2025 17:46


Nearly Half of Older Kiwis Still Write their Passwords on Paper
Posted 9-Jul-2025 08:42


D-Link 4G+ Cat6 Wi-Fi 6 DWR-933M Mobile Hotspot Review
Posted 1-Jul-2025 11:34


Oppo A5 Series Launches With New Levels of Durability
Posted 30-Jun-2025 10:15









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.