0

I need my site to occasionally read a file in order to update itself. (Daily or weekly if possible)

Is php capable of this alone? (From what I've seen the answer is "no")

What else can I use to do this? (Sql seems like it might work, but I've searched and can't tell for sure)

Junior Mayhé
  • 16,144
  • 26
  • 115
  • 161
user474632
  • 311
  • 1
  • 3
  • 11
  • http://stackoverflow.com/questions/723791/what-are-best-practices-for-self-updating-phpmysql-applications – Xorlev Oct 13 '10 at 15:40

4 Answers4

3

You could use a cronjob that executes your php-script at a given interval (eg every day at 12pm).

In case you cannot use real cronjobs: there are also some sites that offer free "cronjobs" - you give them a URL and they visit it at the time you tell them to. Just google for "free cronjobs".

nocksock
  • 5,369
  • 6
  • 38
  • 63
  • 1
    There is a second alternative to real cronjobs - simulated cronjobs. Each time a user visits a site they themselves can be the trigger for the 'cron job'. A table holds cron information (frequency to run, command to run), and if the current time minus last time the script was run exceeds the interval execute the command. Of course the down side to this is that if no-one visits the site, the 'cron job' will not run. – Seidr Oct 13 '10 at 15:45
  • 1
    I actually already thought of updating when the page loads. I turned the idea down because I'm looking for consistency rather than convenience. – user474632 Oct 13 '10 at 16:17
  • @Seidr I too had similar thoughts, but then one more problem came to my mind, which is if we make any user other than admin the trigger for cornjob, there are a few caveats, 1. the mentioned job that need to be run on intervals could take unusually long time to execute up-to completion, which may annoy the user and make him close the tab which means the task wouldn't run to its entirety. 2. It could output somethings like errors/exceptions that the user never expected, cause he just wanted to do his simple stuff, but didn't know there was a whole lot of things going on in the background. – Mohd Abdul Mujib Jul 27 '14 at 02:05
  • That's true, however you'd be able to fire them off into a thread of their own by extending the Thread class. That should also prevent any output from being returned to the user. If you were that worred, you could also us ob_start and ob_end_clean to store/clear the output buffer stream during/after the cron job has run. – Seidr Jul 28 '14 at 10:22
1

Cronjobs are the obvious answer (for linux based servers). However for people who don't have the ability / permissions to do this on their paticular environment.

The other option is to build a simulated cron. Which basically executes a script every time the site / page is loaded. This could then check the current time / date and decide whether it needs to perform further operations, in your case the update operation.

David Cooke
  • 141
  • 1
  • 7
0

You can use crontables on unix system to run periodical updates.

Aif
  • 11,015
  • 1
  • 30
  • 44
0

Use Cron:

crontab -e

Then you edit the file like in vim or vi.

http://blog.linuxvin.com/wp-content/uploads/2010/07/crontab-syntax.gif

This would execute everyday:

0 0 0 0 0-6 /absolute/path/to/the/executable

If you want to make your php script executable use the ol' she-bang. Add:

#!/usr/bin/php

or whatever the path is to your php interpreter to the first line of your PHP file and then do:

chmod a+x yourfile.php
KeatsKelleher
  • 10,015
  • 4
  • 45
  • 52