Im using database controlled cronjobs for my newsletter. Currently crontab job add / remove functions works fine, but when remove a job on list main trigger file does not work properly. So here my 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) is checking this table, if date is reach to current date its add cron_command to crontab, everything works fine here. tasks.php file works fine and send all "ID1" campaign mails.
After campaign task is done and all 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 and should have remove record both side (crontab / db table). But its does not remove command on crontab and db table, also 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 this class, array instead of string would be the standard input / output format.
// Legacy way to add a job:
// $output = shell_exec('(crontab -l; echo "'.$job.'") | crontab -');
static private function stringToArray($jobs = '') {
$array = explode("
", trim($jobs)); // trim() gets rid of the last
foreach ($array as $key => $item) {
if ($item == '') {
unset($array[$key]);
}
}
return $array;
}
static private function arrayToString($jobs = array()) {
$string = implode("
", $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 * FROM tasks ORDER BY 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 FROM tasks WHERE 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 my issue; cron.php file does not work after add new cron job via shell, but when I try to run cron.php file via browser its works fine. Whats wrong?
Regards.