0

I want to create background process in PHP

I am trying all these with AJAX call on client side and PHP on server side..

Actually i am scrapping lot of urls. To do this, I am listing all the scrap url in text area and submitting form using POST method. The form action will catch the POST data and pass to background script. As i can pass lot of urls, it could be 1000 , just because i want to run script in background on demand and when it complete return me output in a file or database. (Write Process id in file or db, i can do)

I do not want to setup a back end cron job for this which will run after 15 min.

The issue coming with PHP code which we generally used to parse is Until my script execution did not complete i can't close browser or change page. But i want to change page and also i can browser but script should run in background.

My understanding is in this case:

  1. I have a form in page, on form submit, i will call on a PHP script which will run in Background.
  2. Background process started and running ....running ....running .....running......
  3. I want to got back process id with which Process id Background process started and previous background process running ....running ....running .....running ..........

It is behaving like other languages Channel if i am right..

i come to solution with PHP Code: I am assuming this below script will work as i want or i need to find another way ?

 $cmd = "php test.php > testoutput.php 2>&1 & echo $!";
 exec($cmd, $pid);
 echo "Script Running with ".$pid;
 exit;

enter image description here

Naresh
  • 2,761
  • 10
  • 45
  • 78

2 Answers2

0

As per your question and our discussion you don't need to run any cron you want to just call your file on user hit.

You will get that id in file in below way :

 $file      = "/your_path/your_file_name.txt";
 file_put_contents($file, print_r($pid, true));

If you want to append id's

 $file      = "/your_path/your_file_name.txt";
 file_put_contents($file, print_r($pid, true), FILE_APPEND);

Here FILE_APPEND is append other id's in same file every time otherwise it will overwrite or you will generate dynamic file name

Make sure that you give a permission of 777 where you want to store your file

jilesh
  • 436
  • 1
  • 3
  • 13
  • Is this solution will run on background and i can still visit on other page after close this url ? – Naresh Feb 10 '16 at 12:28
  • you want to just put this code after getting $pid from exec() and you will get that id in .txt file at your defined path. also you will get that id using file_get_contents() for more details of this function refer this : http://php.net/manual/en/function.file-get-contents.php – jilesh Feb 10 '16 at 12:30
0

You should not do it like that. Code you are about to do is unreliable, hard to maintain, slow and not how you should do it. Yes you can do as Chetan already mentioned things like this php execute a background process. However, this won't be good approach.

What I suggest you is this:

a.) Create database table containing job description such as url, id, and its status so you can filter later on based on that.

b.) Create aside program that will be run by supervisord or any other service management you're familiar with. On this way you can create script that will pull next available job from database when extra worker is free.

c.) Use primary id of that new table as "job id". You can than use websockets for example to show customers status of the job.

Ideally you would not use database for this but in fact things such as rabbitmq or any other queueing system or even things such as redis.

Hope this helps!

Community
  • 1
  • 1
Nevio Vesić
  • 367
  • 3
  • 7
  • Thanx to give your ans, But i know all these things. Issue is if i will close my browser then this script will stop or if i will change navigation then i will stop .. – Naresh Feb 10 '16 at 12:58
  • If you run script with supervisord as separate entity than it will not. Than same does not have anything with browser and user session. – Nevio Vesić Feb 10 '16 at 13:03
  • hi i added my quesiotn same as this question here http://stackoverflow.com/questions/35336948/how-to-execute-script-in-silent-mode-to-get-rid-from-browser-hang-on – Naresh Feb 11 '16 at 11:29
  • Instead of passthru you should I think use http://php.net/manual/en/function.exec.php . Passthru won't return value back. Just status. – Nevio Vesić Feb 11 '16 at 11:32