1

I am working on twilio api. In which I want to execute a file which says your calling subscription have last 2 min remaining during conference call.

So for that I have remaining time of user lets say 210 seconds. and I have to pass ConferenceSid of current conference call into cron file a parameter.

This file will execute only after specific time after(ex. after 88 sec) of conference call start. So i have to set cron that run after 88 sec of conference call start and delete that dynamically created job once execution completed.

For cron job set I am using following crontab class. And here is my code which I am trying.

$path = dirname(__FILE__);
$cron = $path . "/test.php ConferenceSid=".$_REQUEST['ConferenceSid'];
Crontab::addJob('*/1 * * * * php '.$cron);
Crontab::removeJob('*/1 * * * * php '.$cron);

I don't know how to pass parameter so file execute after 88 seconds.

Here is my Crontab class Which is found from another stack overflow answer.

class Crontab {

    static private function stringToArray($jobs = '') {
        $array = explode("\r\n", trim($jobs)); // trim() gets rid of the last \r\n
        foreach ($array as $key => $item) {
            if ($item == '') {
                unset($array[$key]);
            }
        }
        return $array;
    }

    static private function arrayToString($jobs = array()) {
        $string = implode("\r\n", $jobs);
        return $string;
    }

    static public function getJobs() {
        $output = shell_exec('crontab -l');
        return self::stringToArray($output);
    }

    static public function saveJobs($jobs = array()) {
        $output = shell_exec('echo "' . self::arrayToString($jobs) . '" | crontab -');
        return $output;
    }

    static public function doesJobExist($job = '') {
        $jobs = self::getJobs();
        if (in_array($job, $jobs)) {
            return true;
        } else {
            return false;
        }
    }

    static public function addJob($job = '') {
        if (self::doesJobExist($job)) {
            return false;
        } else {
            $jobs = self::getJobs();
            $jobs[] = $job;
            return self::saveJobs($jobs);
        }
    }

    static public function removeJob($job = '') {
        if (self::doesJobExist($job)) {
            $jobs = self::getJobs();
            unset($jobs[array_search($job, $jobs)]);
            return self::saveJobs($jobs);
        } else {
            return false;
        }
     }
}
mraron
  • 2,411
  • 18
  • 27
Rakesh Sojitra
  • 3,538
  • 2
  • 17
  • 34

2 Answers2

0

You can drop the cron approach and have your script start like this:

sleep($numberOfSeconds)

You just need to call the script in a way that won't block your main program. See How can I run a program in the background (non blocking) with php?

Community
  • 1
  • 1
Nenad Mitic
  • 577
  • 4
  • 12
  • Yes I know that sleep function but if I need to use sleep(50000) or for more seconds than it will affect in CPU performance ? What about if a file run more than once at a time ? It execute independently ? – Rakesh Sojitra Sep 17 '16 at 08:44
  • Well, the script is loaded and running, so it has to use some CPU and RAM, but as far as I know, it's minimal – Nenad Mitic Sep 17 '16 at 08:54
0

I'd use at instead of cron. It is designed to execute a command once.

If you need seconds precision, you can use it with sleep, so instead of sleep(50000) you run at now + 833 min and sleep(20).

Alex Blex
  • 34,704
  • 7
  • 48
  • 75