How to execute a php script every day [duplicate]

若如初见. 提交于 2019-12-04 20:02:32

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

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

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