Kristian Rafteseth
Kristian Rafteseth

Reputation: 2032

How to run a php script in the background?

I have a script, lets call it linkchecker, that loops through about 10.000 URLs, checking them for http status codes. When they are checked, the url is marked as checked in my DB. It wont output anything until its done, which can take many hours.

So I thought about just having another script that will run the linkchecker in the background, while continually polling the DB about how many URLs are checked, so I can follow the progress, and if any URLs are giving a problem with long connection time and so on.

I tried just running the linkchecker in an iframe, but nothing will load until the linkchecker has finished.

How can i execute this linkchecker in the background while the main script runs normally, executing other tasks?

Upvotes: 1

Views: 399

Answers (3)

Seder
Seder

Reputation: 2763

I suggest making an AJAX request to new page lets call it ajaxChecker.php

In thuis page its just see if there is unchecked URLs (return number) If the number of unchecked is zero then echo the output in the new div

function checker()
{
    $.post('ajaxChecker.php',function(data){
    if(data.length > 0)
    $('#result').html(data); 
    });
}

setInterval( "checker()", 10000 );

And of course make a request through ajax or cron job to start it first

Upvotes: 0

Friend of Kim
Friend of Kim

Reputation: 860

In the DB make a column you call "Checked". Just make the PHP-script update which are checked in the database. Use phpMyAdmin to see the database graphically, just choose to sort after the column "Checked" and then you could see how far it has come.

You have to do it this way, because the webpage will not update until the script is done. However you could make the script run another script that says how far the process is, but that would maybe be too time-consuming?

You also have to go into php.ini to check that max_execution_time is set to several hours. 60*60*24 = 1day = 86 400 seconds.

Hope this helps! :)

Upvotes: 0

Ivan
Ivan

Reputation: 15942

You have to set a cron job (if you are running Linux) that executes a curl command to access a PHP script (external, like 'curl http://domain.com/php/something.php') or just executing a php command pointing to a internal file.

You can make a scheduler that executes every minute (that's the minimum execution time supported by cron job) and executes a "block" of your work. Of course, you must set PHP to skip the 30 seconds execution limit used by default.

Upvotes: 2

Related Questions