douxie2007 2018-09-15 15:09
浏览 62
已采纳

如何在cakephp中设置cronjobs

I'm trying to setup a cronjobs in cakephp3, I can create a command class

class RescanCommand extends Command { 
public function sendMail() {
    $email = new Email();

    // Sample SMTP configuration.
    Email::setConfigTransport('mailtrap', [
        'host' => 'smtp.mailtrap.io',
        'port' => 25,
        'username' => 'username',
        'password' => 'pass',
        'className' => 'Smtp'
    ]);

    $email->setFrom(['test@test.com' => 'CSV file'])
        ->setTo('test@test.com')
        ->setSubject('CSV Link File')
        ->send('Please find attached a copy of the links');
}

public function execute(Arguments $args, ConsoleIo $io) {
    $this->sendMail();
}
} 

in order to setup the cronjob, I have to login to cpanel and create a cronjob in the format

https://sontest.000webhostapp.com/myscript.php

for setting up the cronjob in cakephp I have to write

          • cd /Application/MAMP/htdocs/music && bin/cake rescan execute

my question is how can I make the cronjob for the server and access it through the cpanel, I need to have the script in the webroot to access.

any help would be appreciate it.

  • 写回答

1条回答 默认 最新

  • doulangbi6869 2018-09-15 15:55
    关注

    I dont know if there exists any "canonical" way of doing this, but I have good results with approach based on creating a modified bin/cake.php file and putting it in directory required by hosting provider (it can be any directory inside or outside application directory). An example file below:

    #!/usr/bin/php -q
    <?php
    require 'path_to/config/requirements.php';
    require 'path_to/vendor/autoload.php';
    
    use App\Application;
    use Cake\Console\CommandRunner;
    
    $runner = new CommandRunner(new Application('path_to/config'), 'cake'); //path to your app 'config' dir
    $command = [
        "", //first can be empty (normally it's 'cake')
        "command", //your command/shell name
    ];
    exit($runner->run($command));
    

    You should also remember, that files defining cronjobs shouldn't be accessible by everyone (like putting it in webroot and making it accessible over web), so you should also implement something that allows access to it only by service that runs it on your hosting.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?