How to run the PHP script at scheduled time

2019-01-09 18:09发布

问题:

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

Is it possible to write some service in XAMP server to run the script daily at scheduled time? I have no idea how to update database and send email automatically at the scheduled time. Can any one share some ideas or concepts?

I am using PHP and MySQL running in Linux Server.

回答1:

You should use a Cron job to do it. Check out the examples on the Wikipedia page.

The Cron Job should call a script using the php executable that runs the necessary task.



回答2:

Just create the script that does the required job, test it by hitting the URL in your browser once you are sure it works right. Copy the URL and add a Cronjob

Then schedule this command to run at whatever time 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 your script every 4 hours, and assuming your script is at http://localhost/work/scripty.php and assuming that your http root is /var/www,

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

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

If you need more information just comment I will update the answer.



回答3:

PHP cannot run script by itself,since php doesn't have daemons like python !! So you have to take OS help 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" (service is the parameter passed to your cronfile).

For setting up cron jobs ,have a look at this link http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/



回答4:

In Linux, We can create .sh file and can give a specific time to run that is called cron job. SO should use this method just make a shell file and give a time period to it. You should take a help with linux expert for that.

Use the following: Cron Job



标签: php mysql linux