Cron 作业通过 CLI 命中控制器的命令

Command for Cron job to hit controller through CLI

我想通过 cron 作业来打这个控制器

<?php

 set_time_limit(0);
include 'vendor/autoload.php';
 include 'endroid/vendor/autoload.php';
 include 'lib/FPDI_Protection.php';

 use Endroid\QrCode\QrCode;

 class pdf extends CI_Controller {

public function __construct() {
    parent::__construct();

    $this->load->model('product_model');
}

 public function index() {
    $hDB = new Mysqli('localhost', 'root', '', 'new');
    $result  = $hDB->query('SELECT * from requests order by requests.request_id asc limit 1');
     while ($row = $result->fetch_assoc()) {
        $pro_id = $row['id'];
        $request_id =  $row['request_id'];
        $product = $row['product_id'];
        $quantity = $row['batch_quantity'];
        $new = $row['new'];
        $batchpath = './output/' . $product . '/';
        $batchpath1 = './output/' . $product . '/';
        if ($new == 1) {
            $rand = $quantity;
        } else {
            $rand = 0;
        }
        @mkdir($batchpath);
        for ($i = 0; $i < $quantity; $i++) {
            $qr_content = $product . '-' . $rand;
            $qrCode = new QrCode();
            $qrCode
                    ->setText($qr_content)
                    ->setSize(97)
                    ->setPadding(5)
                    ->setErrorCorrection('high')
                    ->setForegroundColor(array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0))
                    ->setBackgroundColor(array('r' => 255, 'g' => 255, 'b' => 255, 'a' => 0))
                    ->save($batchpath . '/' . $i . '.png');
            $rand++;
        }
        for ($i = 0; $i < $quantity; $i++) {
            $qr_content = $product . '-' . $rand;
            $im = @imagecreate(300, 90)
                    or die("Cannot Initialize new GD image stream");
            $background_color = imagecolorallocate($im, 255, 255, 255);
            $text_color = imagecolorallocate($im, 0, 0, 0);
            imagettftext($im, 50, 0, 10, 59, $text_color, './captcha_fonts/myfont.ttf', $qr_content);
            imagejpeg($im, $batchpath1 . 'qr' . $i . '.png');
            system('montage ' . $batchpath1 . 'qr' . $i . '.png ' . $batchpath1 . $i . '.png -tile 2x1 ' . $batchpath1 . 'final' . $i . '.png');
            $rand++;
        }

        if ($new == 0) {

            system('montage ' . $batchpath1 . 'final*.png -tile 4x4 ' . $batchpath1 . $product . '.png');
            system('convert ' .$batchpath . $product . '*.png '.$batchpath . $product . '.pdf');
        } else {
            system('montage ' . $batchpath . 'final*.png -tile 4x4 ' . $batchpath1 . $product . '_new' . $quantity . '.png');
            system('convert ' .$batchpath . $product . '_new*.png '.$batchpath . $product . '_new' . $quantity . '.pdf');
        }
        if ($new == 0) {
            $arr['pdf'] = $product . '.pdf';
        } else {
            $arr['pdf'] = $product . '_new' . $quantity . '.pdf';
        }
        $arr['product_id'] = $product;
        $arr['status'] = 1;
        $this->product_model->insert_pdf($arr);
        $this->product_model->request_status($pro_id);
        $this->product_model->delete_request($request_id);
        $pass = $this->product_model->pdf_password();
        if ($new == 1) {
            $origfile = $batchpath1. $product . '_new' . $quantity . '.pdf';
            $destfile = $batchpath1 . $product . '_new' . $quantity . '_crypt.pdf';
        } else {
            $origfile = $batchpath1 . $product . '.pdf';
            $destfile = $batchpath1 . $product . '_crypt.pdf';
        }
        $this->pdfEncrypt($origfile, $pass['pdf_password'], $destfile);
        pdfEncrypt('./output/' . $product . '.pdf', 'slinfy007', './output/' . $product . '_crypt.pdf');
        $rand++;
    }
}

这就是我在 cron 作业命令中写的内容
* * * * * /usr/local/bin/php [file_path]index.php cli/pdf

我认为我的 cron 命令有问题,因为当我直接点击 php index.php pdf 在 cli 中工作正常

* * * * * /usr/local/bin/php [file_path]index.php cli/pdf

只需从 cli/pdf 中删除 cli 并删除 /usr/local/bin/ 你的命令应该如下所示

* * * * * /php [file_path]index.php pdf