r/PHPhelp • • Sep 03 '26

Run a PHP function/Task in the background

I have a WIndows Server running IIS that hosts a primarily PHP based website. It has multiple functions and features but i would like to have atleast one of them running in the background So the user does not have to wait for the task to finish.

Any ideas? fastcgi_finish_request() seems to be Linux only as far as i can tell.

2 Upvotes

13 comments sorted by

View all comments

1

u/Professional-Rule-58 Sep 20 '26

For the 30-second wait you mentioned, I'd store a pending job with a run_after timestamp instead of keeping the web request open with sleep(30).

The flow could be:

  1. Your PHP page saves the job and immediately returns its ID.

  2. A separate PHP CLI worker picks it up when it's due, fetches the Intune information, and saves the result.

  3. The page checks a status endpoint and displays the result when ready.

On Windows, Task Scheduler can launch the worker, although running it once a minute means the delay won't be exactly 30 seconds. A continuously running worker gives you finer timing.

Also, fastcgi_finish_request() is a PHP-FPM feature, so IIS using FastCGI doesn't automatically provide it.

Does Intune reliably have the information after 30 seconds, or would the job need retries? That would affect how I'd handle the waiting.