codeigniter 如何通过从文件中阅读来动态添加cron作业,而不考虑服务器

jgzswidk  于 8个月前  发布在  其他
关注(0)|答案(4)|浏览(47)

我想添加cron作业动态一旦用户安装php应用程序在他们的服务器上,如管理配置,我需要设置cron作业动态一旦去思考其配置设置在php?我使用codeigniter设置cron作业,我也从cpanel手动添加了相同的,它工作正常。

byqmnocz

byqmnocz1#

可以在中创建文件
/etc/cron.d
并使用PHP的file_put_contents()更新文件。但是,您需要提升PHP或Apache的权限(取决于您的PHP处理程序)。不建议这样做,因为它会导致安全问题。
如果使用Cron运行PHP脚本,那么可以每隔X次使用PHP直接调用脚本。在数据库中创建一个变量,表示脚本的最后运行时间。如果自脚本运行以来经过了X时间,则运行脚本并更新变量。如果脚本需要很长的时间来执行,那么使用PHP在一个单独的进程中运行它,这样用户就不必等待它完成。
谢谢

vxf3dgd4

vxf3dgd42#

首先,您必须创建一个txt文件,例如crontab.txt,然后您必须使用shell脚本,如下所示

exec ( 'sudo crontab -u apache -r' );
file_put_contents ( '/var/www/html/YOUR_PROJECT/crontab.txt',"25 15 * * * php /var/www/html/YOUR_PROJECT/YOUR_CONTROLLER/YOUR_METHOD'.PHP_EOL);

最后,你必须执行这个文件,就像

exec ( 'crontab /var/www/html/YOUR_PROJECT/crontab.txt' );

希望这对你有帮助。

sg24os4d

sg24os4d3#

你可以使用shell脚本

shell_exec('echo "25 15 * * * <path to php> /var/www/cronjob/helloworld.php > /var/www/cronjob/cron.log" | crontab -')

您可以使用这些函数或类

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("\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;
      }
  }
 }
6tr1vspr

6tr1vspr4#

谢谢你的回复,我现在得到了我的答案,根据回复,我试图使用php(codeigniter)添加一个新的cronjob到crontab文件:以下是我的答案

$phppath =  exec('which php');
$user_file_path =  getcwd();
$cronjob1 = "0 0 1 * * $phppath  $user_file_path/index.php 
automatic_updates/leave_update_cron";
// run each cron job
// get all current cron jobs
$output = shell_exec('crontab -l');
// add our new job
file_put_contents('/tmp/crontab.txt', $output.$cronjob1.PHP_EOL);
// once append the job, execute the new file
exec('crontab /tmp/crontab.txt');

这将添加一个新的cronjob,而不会删除当前cronjob文件中的任何内容

相关问题