Skip to content

Instantly share code, notes, and snippets.

@vrushank-snippets
Created July 31, 2014 06:55

Revisions

  1. vrushank-snippets created this gist Jul 31, 2014.
    25 changes: 25 additions & 0 deletions PHP: Dynamic Cron
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    if(isset($_POST['matching_algo_run_time']) && !empty($_POST['matching_algo_run_time'])){

    $algo_time = explode(':', $_POST['matching_algo_run_time']);
    $hours = $algo_time[0];
    $minutes = $algo_time[1];
    $hour_12 = ($hours > 12) ? ($hours + 12) - 24 : $hours + 12;

    if($hour_12 == 24)
    $hour_12 = '00';

    $output = shell_exec('crontab -l');
    $cron_file = '/tmp/crontab.txt';

    if(file_exists($cron_file)){
    unlink($cron_file);
    echo exec('crontab -u www-data -r');
    }

    $cron1 = "$minutes $hours * * * curl -s -m 10 http://localhost/one_degree/admin/welcome/send_email";

    $cron2 = "$minutes $hour_12 * * * curl -s -m 10 http://localhost/one_degree/admin/welcome/send_email";

    file_put_contents($cron_file, $output.$cron1.PHP_EOL.$cron2.PHP_EOL);
    echo exec('crontab /tmp/crontab.txt');
    }