I have 2 PHP files. One file is caller.php and the other is worker.php
caller.php will start worker.php on the (linux) system and caller.php should be ended immediately (while worker.php is still working on the server)
The worker.php is taking a lot of time and it will write the status to a database or a file.
I want to be able to open caller.php in the browser which starts ‘php worker.php’, close the browser, come back in 5 minutes and check the status.. (or the script will send a mail after completion) – any ideas how to do this?
You can end client connection and still continue processing, all done. You may also want to increase php timeout if processing takes more time than php/httpd global config allows. See
set_time_limit();.This is not exactly what you asked but I think it may be X/Y problem so here we use technique that can be used to:
So basically this answers to your described needs instead of "how to do X?".
Caller
Not forking or killing
callerto leaveworkerrunning alone?That’s right, and that’s because there’s no need to.
Yes, you asked for behavior where "caller.php will start worker.php on the (linux) system and caller.php should be ended immediately (while worker.php is still working on the server)". However, there is no need to do it that way if you don’t want to do multithreading-singleuser app with php. Just let users 1. start something, 2. leave it running, 3. disconnect users, 4. go back to start where users can start more something or quit if bored enough already.
Of course you can replace everything after
flush();with anything, difference is that clients does not anymore listen so that all output goes to black hole named /dev/null. For example test it with something like this:See http://php.net/features.connection-handling for more information.
Also read http://php.net/function.flush if there seems to be any problems with flushing buffer and closing user connection.
There is also another question (with answers) about Why does PHP not support multithreading?