Wednesday, August 8, 2012

Triggering an Asynchronous Process from PHP, Without Waiting to Complete

Triggering an Asynchronous Process from PHP, Without Waiting to Complete

Sometimes, we need to send a large amount of emails, sms or pushnotification from current script & the current page waits until al the stuffs are done.

To, overcome this thing, we can simply trigger a new process asynchronously and pass parameters to it, without making any wait conditions. The process will start and take its time to complete, without affecting the parent script.

we can use exec() or system() function for that. The difference is System() also prints the output.
Structure- exec('PATH/TO/PHP PATH/TO/FILE.PHP');

1. Here is a sample command to call another script from current file as a separate process-

exec('/usr/bin/php5 /var/www/vhosts/');

2. Doing the above will start the script as a new process, but the parent script will still wait - this process to complete. To overcome that we will modify our command to this-

exec('/usr/bin/php5   /var/www/vhosts/ > /dev/null 2>&1 &');

3. Now, the process will execute, without keeping the parent to wait. But, we surely need to pass parameters to the process Script. That will be simply accomplished using $argv. $argv returns an array of arguments passed to script.

In this example, we will pass a parameter: 15 to the processScript.php-

exec('/usr/bin/php5   /var/www/vhosts/  15 > /dev/null 2>&1 &');

- we are done.
- In processScript.php, we will get the parameter: 15 by-

$paramID = $argv[1];  //$argv[0] is always the name that was used to run the script

- and that's all- we need to run a new PHP process. Please post a comment if you have any query or if you like the post.


  1. Hello there! Do you use Twitter? I'd like to follow you if that would be ok. I'm undoubtedly enjoying your blog and
    look forward to new posts.

    Stop by my site: green coffee bean extract for weight loss

  2. This doesn't really have to do with the topic taking place, but I must ask if you know where I could obtain a quality captcha plugin that I can implement on my blog?? I'm utilising
    the same blog system as yours and I'm having difficulty finding one?

    Also visit my blog:

  3. Howdy, I spotted your blog on http://shareprob. while looking for a related topic, your website came up, it seems good.
    I've saved it in my google book marks.

    Also visit my web page ... weight loss pill

  4. No doubt, this is great. It's not every day you come upon a blog that's
    this good on web 2.0 site, and you've got it down perfectly. Not too many people these days make an effort to put some thought into this issue. Now I’m very happy I found this during the course of my search for anything pertaining to this.

    Also visit my web blog; cinq

  5. Excellent post! We are linking to thiѕ ρarticularlу great сontent on οur wеbsite.

    Keеp up the great wгіting.

    Feel free to surf tο my weblog ... diamondlinks review