How to run a PHP script at a scheduled time

I need to run a php script at a scheduled time daily in order to update some fields in the database and send automatic email. How can i do this?

Can I write some service on the XAMP server to run the script daily at the scheduled time? I have no idea how to update the database and send emails automatically at the scheduled time. Can anyone share some ideas or concepts?

I use PHP and MySQL running on Linux Server.

+4
source share
4 answers

For this you must use Cron . Check out the examples on the Wikipedia page .

The Cron job should invoke the script using the php executable executing the necessary task.

+3
source

Just create a script that performs the required task, test it by clicking the URL in your browser as soon as you make sure that it works correctly. Copy url and add cronjob

Then plan this command to run anytime you want to run

 php ABSOLUTE_URL_TO_SCRIPT >> logfile 

The log file is optional. But it will give you a chance to see what happened.

For example, if you want to run a script every 4 hours, and if your script is located at http: //localhost/work/scripty.php and it is assumed that your root is http - / var / www,

you would run "crontab -e" in the terminal and add the following line:

 * */4 * * * php /var/www/work/scripty.php 

If you need more information for comment only, I will update the answer.

+2
source

On Linux, we can create a .sh file and give a specific time to start, which is called the cron job. SO should use this method by simply creating a shell file and providing it with a period of time. You should seek help from a linux specialist.

Use the following: Cron Task

0
source

PHP cannot run the script on its own, since php does not have daemons like python !! Therefore, you need to take the OS to invoke your custom script.

For example, in linux: (Example.sh) export USE_PHP = php cd $ SCRIPT_ROOTDIR $ USE_PHP -f cronfile.php service = "checkdatabase" (a service is a parameter passed to your cronfile).

To configure cron jobs, check out this link http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/

0
source

Source: https://habr.com/ru/post/1389213/


All Articles