开发者

Using CronTab to run php scripts

开发者 https://www.devze.com 2023-02-13 13:39 出处:网络
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

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 includ开发者_开发知识库ing 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


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

* * * * * wget http://localhost/myscript >/dev/null 2>&1


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


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

精彩评论

暂无评论...
验证码 换一张
取 消