Forum Moderators: coopster

Message Too Old, No Replies

how to daily automatically execute a php file

my custom XML reader needs to do auto DB insert

         

kevinkp7

2:46 pm on Apr 20, 2009 (gmt 0)

10+ Year Member



I have written a function to read the XML Feeds of my online community's Blogspot blogs to find the people who have posted something new in the last 3 days.

My question is - how do I execute a php file automatically every 12 hours which would insert the most recent posts into my database?

I can write the insert but I don't know how to execute a php file automatically every 12 hours.

FourDegreez

3:56 pm on Apr 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Set a cron job. If you can SSH to a command prompt on your server, type crontab -e otherwise there may be another way to set it, such as through cpanel.

You can look up how to write a cron job entry, but it looks like this:

0 0,12 * * * /usr/bin/php -q /home/[your_user_account]/[path]/[to]/[script].php >> /home/[your_user_account]/.cronlog 2>&1

This part tells cron to execute your script at midnight and noon: 0 0,12 * * *

This is the path on your server to the php executable: /usr/bin/php

This part at the end tells cron to append any output (or errors) from the script to a file .cronlog in your home directory: >> /home/[your_user_account]/.cronlog 2>&1

It's not a good idea for a scheduled script to output a lot of data or else this file will grow very large.

---

If you don't have access to cron, a quick and dirt way I've used in the past to accomplish this is to take a popular script, say your homepage, and have it call a function that checks a small file. This file has a single value written in it, let's say, microtime(true) + 12 hours. The function reads the file and checks if the current microtime is greater than the value in the file. If yes, execute the periodic script you want to execute and overwrite the file with a new microtime + 12 hours.

The flaws in this method are obvious, but it can work if you have no other option.

bkeep

4:08 pm on Apr 20, 2009 (gmt 0)

10+ Year Member



Create a cron job if running Linux

0 12,0 * * * /Path/to/script

AndrewCF

2:30 am on Apr 23, 2009 (gmt 0)

10+ Year Member



If you are running in Linux, create a cron job.

If you are running in Windows, use Windows Task Scheduler.

kevinkp7

1:05 pm on Apr 23, 2009 (gmt 0)

10+ Year Member



I don't actually have access to the server directly - it's through a php web host -