Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run PHP script in certain interval (e.g. once a day)?

Tags:

php

autorun

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

like image 386
Adriana Avatar asked May 14 '09 19:05

Adriana


People also ask

How do I make PHP scripts run forever?

For allowing to run the script forever and ignore user aborts, set PHP inbuilt function ignore_user_abort(true). By default, it set to False which throws fatal error when client aborts to stop the script.

How many ways in which PHP scripts can be executed which?

Executing PHP files ¶ There are three different ways of supplying the CLI SAPI with PHP code to be executed: Tell PHP to execute a certain file. Both ways (whether using the -f switch or not) execute the file my_script.


1 Answers

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());
    }
}
like image 105
cOle2 Avatar answered Oct 27 '22 00:10

cOle2