7

I have a script that uses while(true) to run so it runs forever until it dies. I want to be able to make it echo something once every 4 minutes, how can i do this? The script runs on command prompt and it uses while(true) so its confusing plus i am not sure how to make it do that every 4 minutes. How can i make it echo something once every 4 minutes while still in a while(true)?

2
  • maybe sleep or delay function
    – rsz
    Oct 13, 2012 at 22:45
  • 1
    Are you trying to spawn a server process to do some work from PHP or is this a local script that you are executing?
    – jheddings
    Oct 13, 2012 at 22:51

3 Answers 3

7

You can try

while(true)
{
    sleep(240); // sleep for 240 sec
    echo " Hello World" ;
}

Or

$time = time();
while ( true ) {
    /*
     * Play Some Ball
     */

    if ((time() - $time) >= 240) {
        echo date("Y:m:d g:i:s"), PHP_EOL;
        $time = time();
    }
    sleep(2);
}

Output Test with Time = 2 sec, Sleep = 1 sec

2012:10:14 12:50:56
2012:10:14 12:50:58
2012:10:14 12:51:00
2012:10:14 12:51:02
2012:10:14 12:51:04
2012:10:14 12:51:06
2012:10:14 12:51:08
2012:10:14 12:51:10
2012:10:14 12:51:12
2012:10:14 12:51:14
5
  • 2
    i cant make it sleep because i need it active. Oct 13, 2012 at 22:47
  • 2
    I would always recommend a small sleep on a tight loop that will run forever. That or some other yielding method to avoid an all-consuming loop.
    – jheddings
    Oct 13, 2012 at 22:55
  • @jheddings i totally agreed with you ... Nice one
    – Baba
    Oct 13, 2012 at 23:00
  • @jheddings Too bad have exceeded by daily reputation ... Thanks :)
    – Baba
    Oct 13, 2012 at 23:03
  • 1
    yup small sleep to give job scheduler a change using something like usleep.
    – Alfred
    Oct 13, 2012 at 23:13
4

Using a sleep method will actually halt your script from running. I'm not 100% if this is what you want to happen.

Another way to attack this issue would be to compare timestamps from the last "echo" command on each iteration.

$echo_time = time();
$interval = 4*60;
while(true){
  if ($echo_time + $interval >= time()){
    echo "$interval seconds have passed...";
    $echo_time = time(); // set up timestamp for next interval
  }
  // other uninterrupted code goes here.
}

This will allow your code within your loop to continue running and only check the times at the start of each iteration.

1
  • In php7 echo "$interval seconds have passed..."; should be: echo $interval . "seconds have passed..."; Or: echo $interval . 'seconds have passed...'; // single quot is faster than double quot
    – jagb
    Mar 4, 2017 at 18:10
1

Try adding in a loop of while(true) { ... } sleep() parameter function.

$sleep = 4*60;
while(true)
{
    # waiting...        
    sleep($sleep);
    # work after 240 mins
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.