Forum Moderators: coopster
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.
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.