php - Crontab file is corrupted after remove an job on list? -
im using database controlled cronjobs newsletter. crontab job add / remove functions works fine, when remove job on list main trigger file not work properly. here workbench;
task list table;
id campaign_id remove date cron_command 1 1 0 2015-04-22 17:00:00 * * * * * curl -s http://example.com/tasks.php?id=1
main trigger file (cron.php) checking table, if date reach current date add cron_command crontab, works fine here. tasks.php file works fine , send "id1" campaign mails.
after campaign task done , mails sent successfully, above record updated to
id campaign_id remove date cron_command 1 1 1 2015-04-22 17:00:00 * * * * * curl -s http://example.com/tasks.php?id=1
main trigger file check remove 1 marked commands , should have remove record both side (crontab / db table). not remove command on crontab , db table, main trigger file gives error "php command error: no input file specified", "curl command error: 400 bad request", "wget command: ambiguous redirect"
crontab manager class
class crontab { // in class, array instead of string standard input / output format. // legacy way add job: // $output = shell_exec('(crontab -l; echo "'.$job.'") | crontab -'); static private function stringtoarray($jobs = '') { $array = explode("\r\n", trim($jobs)); // trim() gets rid of last \r\n foreach ($array $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; } } }
main trigger file cron.php
/* task list modifier */ $modchr = new crontab(); $optasks = $myconn->query("select * tasks order remove desc") or die(mysqli_error($myconn)); while($optasksrs = $optasks->fetch_assoc()){ /* remove crons */ if($optasksrs['remove']==1){ if($modchr->removejob($optasksrs['cron_command'])){ $myconn->query("delete tasks id=". $optasksrs['id'] .""); }else{ echo('command not executed'); } } /* add crons */ else{ if($optasksrs['date']<=date('y-m-d h:i:s')){ $modchr->addjob($optasksrs['cron_command']); } } } $optasks->free();
shortly issue; cron.php file not work after add new cron job via shell, when try run cron.php file via browser works fine. whats wrong?
regards.
Comments
Post a Comment