简体   繁体   中英

How to run the PHP script at scheduled time

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.

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.

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.

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 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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM