daGrevis
daGrevis

Reputation: 21333

How to execute long running tasks in PHP without cron

Lets imagine my situation (it's fake, of course)...

I have web-site that have 1000 active users. I need to do some stuff that will be slow (database clean-up, backups) etc.. For example, it may take 5 minutes.

I want to accomplish that when user opens my web-site and some params are executed (for example, he is first visitor in one week)... user somehow sends signal to server - that slow process must be run now, but he doesn't wait for it's execution (those several minutes)... he just sees notification that server is making some updates or whatever. Any other user that opens my web-site in that time also sees that notification.

When process is executed - web-site returns to it normal behavior. And it all happens automatically.

Is this possible without cron ?

Upvotes: 1

Views: 2073

Answers (3)

DENIEL
DENIEL

Reputation: 185

You are can use CURL for this task. In database or textfile save time last run script and one day in week run this script (of course, update date before execute script):

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,"http://www.exaple.com/service_script.php");

curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);

curl_setopt($ch, CURLOPT_TIMEOUT, 1);

$result_curl = curl_exec($ch);

curl_close($ch);

Upvotes: 0

fire
fire

Reputation: 21531

On your page you could have a database value i.e. script_runninng then check if the value = 0, send the command, if = 1 do nothing. Then make sure you set the value to 1 at the beginning and 0 at the end of your command.

Upvotes: 0

CristiC
CristiC

Reputation: 22698

Check Execution Operators. They may be what you are looking for. Something like this

$execute = `/path/to/php/script.php`;

// If you needed the output
echo $execute; 

Once this script runs, set a flag (could be in the database or a simple write in a file or something). When the script ends, delete this flag.

In all your other pages - check this flag. If it is ON then display the message.

Upvotes: 2

Related Questions