Using CronTab to run php scripts

南笙酒味 提交于 2019-12-05 05:55:27

问题


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

回答1:


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

* * * * * wget http://mysite.com/myscript >/dev/null 2>&1



回答2:


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




回答3:


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!!


来源:https://stackoverflow.com/questions/5134068/using-crontab-to-run-php-scripts

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!