Jump to content

IIS run php background task

Recommended Posts



I've come across the need to run a background task after a user has submitted a form. Which i am having problems running.

the php script calling the backup task does not need to wait for the response from the task.


When i run the following command from cmd from within windows it runs fine:


"c:program files (x86)phpv5.3php.exe" c:/inetpub/wwwroot/backgroundtask.php > c:/inetpub/wwwroot/backgroundtask.log


however when running:


exec('"C:Program Files (x86)PHPv5.3php.exe" C:/inetpub/wwwroot/Honours/bgprocess.php  > C:/inetpub/wwwroot/Honours/bgprocess.log');


It fails to do anything (At the moment it is just creating and writing data into a text file as a proof of concept)


I have also granted cmd.exe access to anyone in case this was an issue.







Link to post
Share on other sites

You can use the second and third parameters of the exec function to get the output and return status to see what the issue is, but that's not a background task. PHP is going to wait for that command to finish before exec returns. It sounds like you want to fork a child process to keep running and do some other things while the parent process ends.



Link to post
Share on other sites

Thanks JustSomeGuy


I'm not sure i'm understanding pnctl_fork 100% from the examples it appears to return back to the user who 'created' the fork?


I'm wanting the system to run script 'without the users knowledge which might take up to 15 minutes to run'


Quick example how i'd call c:/inetpub/wwwroot/backgroundtask.php to be run in the background and the user can continue to browse pages?





Link to post
Share on other sites

These concepts are explained in the concept of multi-threaded programming in general.


All programs run inside of a process. If you open Windows Task Manager, for example, you can see a list of all of the running processes. A program can use multiple processes, and each process can also use multiple threads. You can use pcntl_fork to create a new process. When you do that, the code that you had running is now running in 2 separate processes, with different process IDs. The example in the manual shows how to check the process ID to determine if the current copy of the code is the parent or child process. For the parent process, you would just quit. For the child process, you would include the other PHP file you want to run. There are other examples in the comments. I would recommend reading up on the topic of multi-threaded programming and signal handling if you decide to do much with multiple processes, there is a lot to know.

Link to post
Share on other sites

That's correct, the process control functions are not available on Windows. One option might be to install the PSExec program, which will spawn a child process and then quit (so that exec does not wait for the child process to finish):




Other than that, maybe you could set up a scheduled task to run every minute and check for work that needs to be done, and the end of your script could add a new job to a database table to tell the scheduled task what to do for that minute.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...