2

I have 2 files running on loop, master.php and bot.php:

master.php

<?php
       while (1) {   
            include 'bot.php'; 
            sleep(60);
        }

bot.php

<?php
declare(ticks=1);

define('START_TIME', time());

// lambda uses START_TIME constant
register_tick_function(function() {
    if (time() - START_TIME > 9000) {
        echo 'Script execution halted. Took more than 20 seconds';
        exit(1);
    }
}, true);

     include_once 'onefile.php';
     include 'somefile.php';
     include 'somefile2.php';
?>

I need to ensure that bot.php doesn't take longer than x seconds. If it does I need to gracefully stop/break the bot.php loop but keep the master.php running the infinite loop.

I have tried Josh Trii Johnston's answer from this question but the problem is that it stops everything including master.php loop (which is the file I will initially run and I don't want to stop)

I need to create this timeout because bot.php performs some tasks on remote servers and sometimes (rarely) it time-outs and freezes forever. This timeout is just to ensure that in those cases the loop will break and a new cycle begin.

Community
  • 1
  • 1
ace
  • 313
  • 1
  • 5
  • 19
  • 1
    If you run bot.php via cron, you will not need gracefully stop/break the bot.php and you can use method that you already found then. So - as solution - just do not use master.php. Use cron for bot.php instead. – Vladimir Gilevich Sep 03 '15 at 12:53
  • We are on a windows VPS.. am not sure if its even possible to make cronjobs here – ace Sep 03 '15 at 13:12
  • It was! I used this http://stackoverflow.com/questions/4701861/how-do-i-run-a-php-script-using-windows-schedule-task. Make a answer telling that and I will award you if you want. TY – ace Sep 03 '15 at 13:24
  • You also can do it like `$result = shell_exec('php bot.php');` – Fedir Petryk Sep 03 '15 at 13:31

0 Answers0