dtcd27183
2015-06-06 09:08
浏览 48
已采纳

使用PHP和Apache创建一个cron作业

I have Apache running on Debian with PHP 5.4.
PHP-cli is installed.
My directory structure for the web project is:

 - /myproject
     - /src
         - /controller
             - getProviders.php
         - /model
         - /public
         - ClassLoader.php

I want to create a cron job to execute getProviders.php every 5 minutes. This is as far as I have come:
*/5 * * * * /usr/bin/php /var/www/myproject/src/controller/getProviders.php

It doesn't work because I have a require_once in getProviders.php requiring ClassLoader.php, but he can't find it.
require_once "../ClassLoader.php"

getProviders.php works when executed via URL.

I'm not new to PHP development, but new at configuring the server around it. What do I have to do to make it work. I'm guessing I have to set the include path, but I have no idea to what exactly.

Thanks in advance for your help.

图片转代码服务由CSDN问答提供 功能建议

我在Debian上使用PHP 5.4运行Apache。
安装了 PHP-cli。
\ nMy web项目的目录结构是:

   -  / myproject 
  -  / src 
  -  / controller 
  -  getProviders.php 
  -  / model 
  -  / public 
  -  ClassLoader.php 
   
 
 

我想创建一个cron作业,每5分钟执行一次getProviders.php。 这是我来的:
* / 5 * * * * / usr / bin / php /var/www/myproject/src/controller/getProviders.php

它不起作用,因为我在getProviders.php中有一个require_once需要ClassLoader.php,但他找不到它。
require_once“../ClassLoader .php“

getProviders.php在通过URL执行时有效。

我不是PHP开发的新手,但是新的 围绕它配置服务器。 我需要做些什么才能使它工作。 我猜我必须设置包含路径,但我不知道到底是什么。

提前感谢您的帮助。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douchengjue9892 2015-06-06 09:13
    已采纳

    To make sure the require_once will always work, you could use dirname(__FILE__) + the path relative to the getProviders.php

    In your case this would be:

    require_once(dirname(__FILE__)."/../ClassLoader.php");
    
    点赞 打赏 评论
  • dtd793353 2015-06-06 09:14

    Change to the right workdirectory.

    You could do this with the cd command or inside your PHP script with the chdir() function.

    点赞 打赏 评论
  • dongzi1209 2015-06-06 09:22

    Create a shell script like this in /usr/sbin (For Example: getProviders)

    #!/bin/bash -x
    cd  /var/www/myproject/src/controller/
    php getProviders.php
    

    give permission

    chmod a+x /usr/sbin/getProviders
    

    in /etc/crontab

    */5 * * * * root /usr/sbin/getProviders
    

    the problem is probably due to inclusions not in absolute value in the php script

    点赞 打赏 评论

相关推荐 更多相似问题