0

I have an AJAX script that takes about 10 minutes to do its thing. I would like to be able to tell the user 'Hey listen, the task is being completed, we'll let you know how it turns out', but it won't let the user run any other scripts on the server until that one completes (I believe this is a consequence of PHP being single threaded, but I'm not sure). Is there a way to assign that AJAX script to a separate PHP or Apache process, so that the user can continue to click around in the application without having to wait for the task to finish?

Matthew
  • 7,305
  • 7
  • 37
  • 38
  • 3
    `I believe this is a consequence of PHP being single threaded, but I'm not sure` Even though PHP is single threaded, every connection in an mod_php environment is a separate process, isolated from and running in parallel with all other processes. – Frank Farmer Sep 21 '12 at 23:42
  • http://stackoverflow.com/questions/2874636/escape-arguments-for-pdo-statements This question, for example, is easily acceptable. – Michael B Sep 21 '12 at 23:46

2 Answers2

3

You can use database or files to insert some lock mechanism to prevent task from running multiple times simultaneously. Then you need to just spawn PHP process using command nohup (no hang up), for more details look at this article: https://nsaunders.wordpress.com/2007/01/12/running-a-background-process-in-php/ or this question: nohup: run PHP process in background .

Community
  • 1
  • 1
Tomasz Kowalczyk
  • 10,385
  • 6
  • 53
  • 66
0

I seek for hours, at least, the solution was very easy for me using Cron Jobs. In cPanel you can go to Advanced -> Cron Jobs, and there schedule a task using a PHP script in Command line.

A Command example that execute a script php:

/usr/bin/wget http://www.example.com/miscript.php

or better:

php /home/USER/public_html/miscript.php
Hernaldo Gonzalez
  • 1,827
  • 1
  • 21
  • 30