Skip to content

Instantly share code, notes, and snippets.

@vrushank-snippets
Created July 31, 2014 06:55
Show Gist options
  • Save vrushank-snippets/fd809625bcdb81358ab4 to your computer and use it in GitHub Desktop.
Save vrushank-snippets/fd809625bcdb81358ab4 to your computer and use it in GitHub Desktop.
PHP: Dynamic Cron
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');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment