How to execute a php script every day [duplicate]

微笑、不失礼 提交于 2019-12-22 00:28:46

问题


Possible Duplicate:
Cron job on Ubuntu for php

I am running and ubuntu server and wanted to run a php script every day. I have done some research and found that cron is the best way of doing this however, this is where i got stuck, a lot of the information on the internet about cron is very hard to follow and understand.

So i wanted to execute a simple php script once a day, the script i made for testing simply just deletes a record from a database, but the real script will do a lot more.

I tried setting up a task through plesk which is provided through my web host service but it didn't seem to execute when i wanted it to, i used 1 for minutes, 22 for hours, * for day, * for week and * for month and thought this would execute every day at 22:01.

I have the directories on my server: cron.hourly cron.daily cron.weekly cron.monthly

I thought i could dump i file in there and it would execute for example every day, but i'm guessing i need to make a cron script to call a php script right?

If i were to go the way of putting a file in the cron.daily folder how would i go about it?

Also if there are any steps i need to take on the php side please let me know?

Thanks a lot for your time.


回答1:


There's couple of ways to setup cron job. Assuming you got shell access you could do crontab -e from console and define job there, i.e. like this:

1 22 * * * command

which would trigger command (whatever it is) at 22:01 each day (not sure why you set minutes to 1 instead of 0 though). To launch PHP script from there you would either have to install php-cli, and then invoke it that way:

1 22 * * * <path>/php -q script.php

You can also call bash script here, to setup all the stuff like paths etc and then call your php script form bash - sometimes it is simpler to do that way instead of crafting way too long command line for cron. And it's simpler to update it later. also, you could turn your php script into bash-runnable script by setting it execution bit (chmod a+x script.php) and adding shell line as 1st line in your script:

#!/usr/bin/php -q
<?php
   ...

If your script got too many dependencies and you'd prefer to call it via web, you could use wget to mimic a browser. so your command would be:

/usr/bin/wget --delete-after --quiet --spider <URL-TO-YOUR-SCRIPT>

wget manual can be accessed by man wget or wget -h, or is on this website. Aternatively you may use HEAD from perl-www package but it requires perl while wget is standalone tool. If you use HTTPS with self signed certs add --no-check-certificate. And you may also want to setup .htaccess and limit web access to your cron script to localhost/127.0.0.1




回答2:


every minute:

* * * * * /path/script.php

every 24hours (every midnight):

0 0 * * * /path/script.php

Se this reference for how crontab works: http://adminschoice.com/crontab-quick-reference, and this handy tool to build cron jobx: http://www.htmlbasix.com/crontab.shtml



来源:https://stackoverflow.com/questions/12215524/how-to-execute-a-php-script-every-day

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