1

With below code i am trying to run script in background. but when hit url in browser then browser is waiting is completion and after this success message is coming. I do not want to hang on browser and do not want to wait for its complete.

Is there any want i can get Process ID (Linux PID) as output when process start. We can say on demand i want to create Process same as Cron job does. but i do not want to setup a Cron job. Because i have 1000 users and each user want to do crawl on demand.

My prev question which is related to this.

How to create background process in PHP

Code on PasteBin: http://pastebin.com/6FkS9ZYx

component:

/
 * @component to write sample script which will pass to backend jobs 
 *
 */

App::uses('Component', 'Controller');
class BackgroundScriptComponent extends Component {

    public function crawllUrls($crawling) {   // Passing 500 urls to crawl

        for ($i = 0; $i < $Counter; $i++) {

            /*
                Internal code which is running perfect using browser hit
            */

        }
    }
}

/* @component to write sample script which will pass to backend jobs */

Controller:

class ScriptController extends AppController {

    public $components = array('BackgroundScript');

    public function CreateBackendProcess() {
        $this->layout = false;
        $this->autoRender = false;

        $arrOfUrls = array('500 url there in this array');

        //echo $this->BackgroundScript->crawllUrls($crawling);  // This line having Component method call. I do not want to call this as same as.. It should call from CLI

        $cmd = "";    //what should come in this $cmd to calls BackgroundScript's crawllUrls() method component which will pass to exec() 

        exec($cmd, $pid);

        echo "<br />Script Running with process id " . json_encode($pid); //I will save this PS ID to database
        exit;
    }
}

enter image description here

Community
  • 1
  • 1
Naresh
  • 2,761
  • 10
  • 45
  • 78

1 Answers1

0

You can create a cronjob which runs every minute. As soon as you submit the form you paste a file to some directory on your website.

Let the cronjob check if the file is there every minute, If the file is there execute a php script (silently as you say) through a shell script, and remove the file.

Daan
  • 12,099
  • 6
  • 34
  • 51
  • Sorry. i cant run Cron job at every minutes .. is there any other solution you can provide. ? I am struggling with this issue from last 5 days. – Naresh Feb 11 '16 at 10:45
  • Why can't you run a cronjob every minute ? – Daan Feb 11 '16 at 10:45
  • also i dont want cron job becuase there could be 1000 users and each user will run script with 50+ urls. – Naresh Feb 11 '16 at 10:49
  • Well yes there is an alternative `passthru("/usr/bin/php /path/to/yourFile.php >> /path/to/log_file.log 2>&1 &");` – Daan Feb 11 '16 at 10:53
  • ok. It will wait till output ? or it will return and let me keep going – Naresh Feb 11 '16 at 10:56