PHP: running a task in background


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
5 views
What are the most correct and reliable approaches should be used if you need to run some long-running task in the background?
I will give an example to make it clear:
there is a main php script, the controller that processes user actions. The user clicks in the interface of the magic button which should start the long-running task and return control to the user. Is not required to return or something to further process the result of a long script.

I know that for such problems there are various libraries to create the message queue, but specifically for my example is redundant.
I would also like to do without cron-task-s and other OS-specific things.

Thank you.
by | 5 views

5 Answers

0 like 0 dislike
I think in your case the simplest option would be to exec:
\r
\r
exec("php /www/site/script.php >>/www/logs/script.log 2>&1 &"); 

Unfortunately this will only work in *nix because Windows start process in background is not so easy
by
0 like 0 dislike
PHP CLI in the background behaving badly. That applies "in a hurry", well-behaved BASH, Perl and Python.
\r
In the task where it was necessary to preserve resources, did
1. The PHP script stores the "job" to BASH in some directory job
2. Kron-the-minute launches a dispatcher script, written in Perl (was a viable option and BASH) who is looking for a N the oldest (by time last modified) scripts from the directory job, and launches them.
3. Run the scripts the first thing destroy (rm) himself, that They still are — because they are already loaded in RAM. The results are stored in filesystem or in a database, to service interface logic
\r
The number N for each server detected individually by testing.
\r
Was "rock bottom" which at first never even was discovered quite time — everything runs Kron should work with absolute paths.
\r
So had to do a project like YouTube and similar, also converting videos.
by
0 like 0 dislike
I also stick to option with a CRON-Ohm.
Personally, I would put it in a table in database where recorded the statuses of the tasks, and the crown continually practiced would be a script (though on php though on anything else), which would cekal this table, start new tasks, and remove the waste.
by
0 like 0 dislike
1. The agents start at the end of page generation.
by
0 like 0 dislike
As an option: the user clicks the button an ajax request on the server runs a php script (ignore_user_abort).
by

Related questions

0 like 0 dislike
7 answers
0 like 0 dislike
7 answers
0 like 0 dislike
1 answer
asked May 10, 2019 by hckn
0 like 0 dislike
1 answer
asked May 12, 2019 by moPhlit
0 like 0 dislike
2 answers
asked Apr 10, 2019 by Alex1237
110,608 questions
257,186 answers
0 comments
28,049 users