How to execute PHP code periodically in an automat

2019-01-12 05:00发布

问题:

I am willing to truncate a table in a MySQL database periodically but I don't know how to make the code that does so run in a specific period of time automatically e.g every week. Do you know any approach or class to do so?

EDIT: I am on windows sever

回答1:

You have two approaches.

First is based on cron jobs - your system may automatically call some PHP script every week. See more in Wikipedia article and learn Cron based on examples (I have seen also some interesting tool somewhere on GitHub, but I am unable to find it at the moment).

The second is to call some task during execution of different script, eg. when you do not have access to crontab. You may eg. store information about executed scripts in the database - if 7 days have passed after last execution, then execute some function / script and save the new timestamp into the database.

EDIT:

You said you are on Windows, but there are cron-like solutions also.

To add scheduled tasks, follow the guidelines from article on Drupal.org.

You can also run Cron-like software, eg. CRONw - Cron for Windows.