10

I have a php script that reads one file through http(the file is on other domain). I would like to read this file only once or twice a day, instead of connecting to it every time the website is refreshed. Is there any other way than doing it with cron? I dont want to use cron cause I prefer to setup this behaviour in the script itself .. so it is flexible, so I can use it anywhere without setting up cron every time. thanks

8 Answers 8

13

I've done this kind of thing in the past when I didn't have access to cron:

$lastRunLog = '/path/to/lastrun.log';
if (file_exists($lastRunLog)) {
    $lastRun = file_get_contents($lastRunLog);
    if (time() - $lastRun >= 86400) {
         //its been more than a day so run our external file
         $cron = file_get_contents('http://example.com/external/file.php');

         //update lastrun.log with current time
         file_put_contents($lastRunLog, time());
    }
}
3
  • thats clever way, nice! However, I will look more on buffering functions since it seems better for what I need.
    – Adriana
    May 15, 2009 at 21:06
  • But you have to run the above code in interval right? I mean you should somehow keep checking the difference between current time and lastRun. How do you do that w/o cron?
    – vmontazeri
    Nov 26, 2017 at 18:40
  • You could have your site visitors run it or call it from another system that you have access to cron on, etc.
    – cOle2
    Nov 28, 2017 at 1:00
7

If you can't or don't want to use use cron and it's ok to update it only when the page is accessed. You could cache the result of the HTTP request and only update it on a page load it if the cache is older than a day or whatever interval you choose.

3
  • thanks, so far you are the only one who understood i seek for other way than cron. how do i do this cache? is APC what you mean?
    – Adriana
    May 14, 2009 at 20:20
  • APC is one way to provide a fast, in-memory cache but not the only way. There's memcached if you want to be able to distribute your cache across mulitple machines. Alternately, you could just insert a copy into the DB and not bring in any new technologies. May 14, 2009 at 20:41
  • 1
    The simplest way to cache things should be to use PHP's built-in output buffering functions. (php.net/manual/en/function.ob-start.php)
    – Alo
    May 14, 2009 at 20:53
2

You could even use a database table - really simple in structure, id, date, script url, and whatever you need - and add a row every time you run the script.

Then, before run the script simply check the numbers of row for each day you have.

1

You can use a Cronjob. You can then run the php script by the command line.

php /someplace/somefile.php

The Cronjob would be the following if you update every day.

0  0  *  0  0  php /someplace/somefile.php
1

Since you explicitly state that you don't want to use cron, the only other way to do this (without something analogous to cron) is to set up your script as a daemon. However, unless you really need the flexibility that daemons provide, cron is much easier and simpler.

Here's one daemon walk-through.

0

If you're using a Linux distro with systemd:

I had a need for scheduling yearly based jobs, independent of the application (in case the system rebooted or anything like that), and I was given the suggestion to use systemd Timers. The Arch Wiki Page on it gives some examples.

-2

If you are on a *nix environment you can use cron jobs

-2

What's wrong with cron?

You have a couple choices with cron - your php can be invoked by the command line PHP interpreter, or you could use wget or fetch or the equivalent to invoke your PHP on the server.

In general, PHP run from within the context of the web server has a time limit on how long it can execute, so in general you can't set up "background" PHP threads to do stuff "later".

1
  • 3
    He specifically asked for an alternative to Cron, this is why I downvoted this answer.
    – Jorjon
    Oct 13, 2012 at 0:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.