【问题标题】:Cron jobs in codeigniterCodeigniter 中的 Cron 作业
【发布时间】:2011-11-11 10:48:56
【问题描述】:

我正在尝试使用 CodeIgniter 中构建的网站执行 cron 作业 - 我可以访问 CPanel cron 功能,谁能建议使用 CPanel 设置 cron 作业的最佳方法?

我正在使用 CodIgniter,所以不确定如何在 cron 作业中调用控制器?

例如http://admin.com/sites/publish/

如何使用 cron 作业在站点控制器中访问此发布功能?

【问题讨论】:

    标签: php codeigniter cron cpanel


    【解决方案1】:

    最好的方法是在 cron 作业中从命令行调用...

    php /path/to/index.php controller >> /dev/null
    

    您可以通过 CI 中的命令行运行控制器,请参阅here

    【讨论】:

    • 您认为这对我的目的是否正确? php /home/username/public_html/index.php/sites/publish >> /dev/null
    • +1 来自我,但我将根据问题和给定您使用php /path/to/index.php sites publish >> /dev/null 的路径添加(意识到您刚刚给出了一个示例,只是想我会澄清一下)
    • 你好 - 刚刚尝试过并且 cron 正在运行,但我收到了以下 Cron Daemon 消息发送给我。注意控制器被称为站点&函数被称为cron /bin/sh: /home/username/public_html/index.php/sites/publish: 不是目录
    • 没有你的命令是错误的它应该是/home/username/public_html/index.php sites publish在index.php之后你发送控制器作为参数。
    • 我没有在你的命令中看到 PHP 的路径,你也可以尝试将参数放在引号中,例如.....php /home/username/public_html/index.php "sites publish"
    【解决方案2】:

    对我来说,更简单的方法是使用 cURL 并在 cron 中执行 url:

    curl http://admin.com/sites/publish/
    

    如果您需要保护 url,您可以使用以下方式通过 post 发送数据:

    curl -X POST -d "apikey=yourapikey&another=variable" http://admin.com/sites/publish/
    

    这样你就不用纠结php参数和不同的配置了。

    【讨论】:

    • 使用这个,你可以将你的 cron 作业暴露给全世界
    • 您可以使用 htpasswd 保护它们。我知道使用 php 而不是 curl 更安全,但是对于一些愚蠢的东西,这种方式更容易。
    【解决方案3】:

    我是这样做的, 创建文件夹 cron

    /application
    /cron
       my_task.php
    /public
    

    为每个 cron 作业 /cron/my_task.php 制作包含内容的脚本

    <?  $_SERVER["SCRIPT_URL"] = "/controllerName/MethodName"; // you can set url in routes if you want
        $_SERVER["HTTP_HOST"] = "your_site_address.com"; // without http://www
        require(dirname(__FILE__) . "/../public/index.php");  // path to index.php
     ?>  
    

    让控制器 Cron 和其他控制器一样,但在 __construct 中添加 IP 验证

    最后像

    一样运行
    1 10 * * * cd /path_to_site_folder/cron/ && usr/local/bin/php /path_to_site_folder/cron/my_task.php >> path_to_log/some.log
    

    【讨论】:

      【解决方案4】:

      对于 Cronjob,试试这个来访问命令行控制器、函数和参数:

      php index.php/controller/function/param1/param2/param3 etc
      

      php index.php controller function param1 param2 param3 etc
      

      【讨论】:

        【解决方案5】:

        只需使用此命令并粘贴即可。

        wget www.example.com/index.php/controller/function
        

        【讨论】:

          猜你喜欢
          • 2013-03-05
          • 2011-01-15
          • 2015-01-21
          • 2012-02-23
          • 2012-08-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多