doushen1026
2011-02-27 15:34
采纳率: 100%
浏览 72
已采纳

使用CronTab运行php脚本

I need to send emails hourly and daily. I've tried nearly everything but it appears my crontab just won't work. If I run the scripts via a browser e.g

http://localhost/Maisha/Functions/sendhourlymails.php  

my emails get sent beautifully.(I moved default website localhost to public_html.) I don't know whats wrong. I read some post in stack overflow including the executable path of php helps hence I've put the /usr/bin/php before the actual script to be cronned will work but it does not. Removing /usr/bin/php does not work. Adding php before the actual script isn't working. I have the following entries in my crontab.

# m h  dom mon dow   command
0  *  *   *   *    /usr/bin/php /home/maxwell/public_html/Maisha/Functions/sendhourlymails.php
0  0  *   *   *    /usr/bin/php /home/maxwell/public_html/Maisha/Functions/senddailymails.php

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

我需要每小时和每天发送电子邮件。 我已经尝试了几乎所有的东西,但看起来我的crontab不起作用。 如果我通过浏览器运行脚本,例如

  http://localhost/Maisha/Functions/sendhourlymails.php 
   
 
  

我的电子邮件发送得很漂亮。(我将默认网站localhost移到了public_html。)我不知道什么是错的。 我在堆栈溢出中读了一些帖子,包括php的可执行路径帮助因此我把/ usr / bin / php放在实际脚本之前,cronned会工作,但事实并非如此。 删除/ usr / bin / php不起作用。 在实际脚本无效之前添加php。 我的crontab中有以下条目。

  #mh dom mon dow command 
0 * * * * / usr / bin / php /home/maxwell/public_html/Maisha/Functions/sendhourlymails.php
0 0 *  * * / usr / bin / php /home/maxwell/public_html/Maisha/Functions/senddailymails.php
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douchun3680 2011-02-27 15:48
    已采纳

    Try to call the script via http with wget like so:

    * * * * * wget http://mysite.com/myscript >/dev/null 2>&1
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douchi0471 2011-02-27 16:21

    Yeh, wget is good option, also you can try to use:

    0 * * * * /usr/sbin/php /usr/bin/php /home/maxwell/public_html/Maisha/Functions/sendhourlymails.php

    but it could work wrong due to relative paths.

    Also you should look at http://php.net/manual/en/features.commandline.php

    评论
    解决 无用
    打赏 举报
  • duanqin4238 2012-01-19 07:04

    Try to put this into your .php file

    <?php
        #!/usr/local/bin/php -q
    //your code here
    ?>
    

    Then if you include any file into this file you must use something like:

    include"/var/www/../your_absolute_path_from_root_folder/connect.php";
    

    Finnaly make sure this file has the right permissions..Try

    chmod 755 /var/www/.../file.php
    

    Then if you edit your crontab file with the following command

    vi /etc/crontab
    

    put something like

    10 6 * * * root php /var/www/..path../file.php
    

    and restart the service with this command

    /etc/init.d/cron restart
    

    you have do your job!!

    • Note-Tip:the php file isn't neccessery to be into public_html folder!!
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题