0

I have a PHP script that runs server side via a CRON job. It functions just fine, but I need it to run every 10 seconds, not 60 seconds the limit of cron. Can I write another "Timer.PHP", that would call/run the UPDATE.PHP, and have the Timer.php loop and sleep to give me the 10 second update time? I could then have the cron run the Timer.php every 2 minutes.

The current cron job is: curl -s http://www.mysebsite.com/template/update/update.php?password=pass I am not sure of the following: 1) is this possible? 2) The correct syntax?

Someone suggested to have it loop 11 times (each loop 10 seconds apart) then stop until the cron calls it again. Their thoughts were to give it a 10 second down time to prevent the cron starting again before the previous one stopped its execution.

Digvijaysinh Gohil
  • 1,002
  • 2
  • 10
  • 25
BobW
  • 1

3 Answers3

0

If you want to run it as a cron job, use php sleep() function. Run a loop 5 times and sleep the script for 10 seconds after execution. e.g.

<?php

for ($x = 0; $x < 5; $x++) {
    // Execute your code
    sleep(10);
}

?>

This is not a perfect solution but it'll work fine.

Kshitij Kumar
  • 310
  • 2
  • 14
  • Is there a better method to use? – BobW Nov 22 '17 at 17:55
  • Yes, you can run your script as a background process. That way you'll have full control on it. – Kshitij Kumar Nov 22 '17 at 18:01
  • I have also been viewing this thread: https://stackoverflow.com/questions/9619362/running-a-cron-every-30-seconds?rq=1 It is leaving me more confused. This is running on a hosted server, so I have no means to control things in the background. – BobW Nov 22 '17 at 18:06
  • If you're on a shared hosting, you should use sleep method as you can't create background process. Just put your update code in a loop. It'll work fine. – Kshitij Kumar Nov 22 '17 at 18:11
  • I am using: it is not working. I have a cron job set to run the above script.....more reading – BobW Nov 22 '17 at 18:22
0

After several failed attempts, I decided it will be simpler to edit the actual PHP file that the cron job is currently properly executing. I will figure out how to loop it 6 times with a 10 second sleep. This will make it run 6 times with a 10 second delay. The cron can then restart the script again.

Thank you all for trying to help yet another newbie Bob

BobW
  • 1
0

What I finally ended up doing is making 6 cron jobs, 5 have a sleep built into them.

1) Normal run 2) Sleep 10; 3) Sleep 20; 4) Sleep 30; 5) Sleep 40; 6) Sleep 50;

Fortunately my hosted server site allows this.
The script takes about 2 seconds to run. Been running this for over six hours so far with no errors. May not be the most glamorous solution, but it works.

BobW
  • 1