Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to periodically execute a PHP script?

Tags:

php

cron

I'd probably figure out a way to do this if I had full access to the server, however the problem is it's just a hosting service which leaves me with nothing but FTP access.

I would like to run a PHP script periodically to check for outdated/broken content, aggregate new content, delete files not in use etc, however the script can take up to 60 seconds to execute (due to aggregation of content) and I feel like an ass to just execute it while processing a request of the first user that visits the website an hour after it's been updated :P

Leaving my home PC on 24/7 to schedule requests is not an option.

like image 350
CookieMonster Avatar asked Jul 28 '11 01:07

CookieMonster


2 Answers

You can use an online cron service to essentially pretend like you have cron access.

Create php file with contents you would like executed

Free Cron Online Website

Set up your free online cron to execute that file every x minutes.

like image 181
Blake Avatar answered Sep 29 '22 22:09

Blake


Not sure if this is the correct approach, but I used to just trigger a script when the first user visits the site, and then send a <meta http-equiv="refresh"... to the user for his browser to refresh the page. The original PHP script would still run on the server, but the user will not see it anymore.

Basically, something like:

if( check if the user is the first visitor today ) {
    set_time_limit(0);
    echo "<meta http-equiv='refresh' content='1;url=..." />"; // put your site baseurl in here

    ... run your scripts here
}

Just an idea. Might not work. Just try it out.

like image 38
Jimmie Lin Avatar answered Sep 29 '22 21:09

Jimmie Lin