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

如何在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.

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

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧