Spen D
Spen D

Reputation: 4345

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.

Upvotes: 3

Views: 9178

Answers (4)

DivinesLight
DivinesLight

Reputation: 2415

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.

Upvotes: 2

Code Lღver
Code Lღver

Reputation: 15603

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

Upvotes: 0

caspersky 48
caspersky 48

Reputation: 239

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/

Upvotes: 0

Ryan Bigg
Ryan Bigg

Reputation: 107728

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.

Upvotes: 3

Related Questions