1

I have two functions. One function I want to run in the background with the mysql connection and without returning any errors or anything to the browser. And another function I want to run which returns data to the browser.

I've used the php pcntl_fork as follows:

$pid = pcntl_fork();
switch ($pid) {
  case -1:
    $this->function_background();
    $this->function_return();
    exit();
  case 0:
    $this->function_background();
    break;
  default:
    $this->function_return();
}

In this case, it returns database error number 2006 which can only occur in function_background().

I want the function function_background() to run completely and independently in the background with the mysql connection and without disturbing the browser with it's errors or anything. And function_return() for a message to the browser.

Appreciate any help. Great if anyone could please point me to detailed info as well.

Thanks.

Mehulkumar
  • 836
  • 2
  • 10
  • 23
  • That's not what the process control functions are meant for. [how to use pcntl\_fork() with Apache?](http://stackoverflow.com/q/12214785) – mario Mar 14 '15 at 12:53
  • Thanks for your answer. Do you have any solution for this except using another programming language? Thanks again. – Mehulkumar Mar 14 '15 at 12:59
  • ignore_user_abort, or schedule a cron job etc. – mario Mar 14 '15 at 13:21

1 Answers1

2

As in the comment the pcnt_fork() is used for fork an existing process, for running it in background you could simply implement something using:

$pid = shell_exec(sprintf('%s > /dev/null 2>&1 &', $command));

where:

  • > /dev/null means that the stdout will be discarded;
  • 2>&1 means that the stderr will be on the stdout (so discarded);
  • & allows to run this command as a background task.

and for check that the process is running

$procResult = shell_exec(sprintf('ps %d', $pid));
if (count(preg_split("/\n/", $procResult)) > 2) {

    return true;
}
nik.longstone
  • 244
  • 2
  • 8
  • Thanks for the answer. Can I run a function as a command and can I pass post data to that function or can function access the post data? In the background function I'm working with post data, some APIs and database. Thanks. – Mehulkumar Mar 14 '15 at 13:37
  • shell_exec or exec() use the shell command line, so you need maybe to have yout processAFile.php that will call all your functionalities. It's like to call from command line your php file. If you chose this approach, I would also like to suggest to do not expose those file to public access, avoid the call from an http url, or if you need that at least you could append a secret token to the file call to increase the security. – nik.longstone Mar 14 '15 at 13:44
  • I understand. Can that file access the post data or any way to pass the post data to that file? – Mehulkumar Mar 14 '15 at 13:46
  • 1
    Tell me if I didn't understand well, do you want to pass post data to that file? if it's yes you could append the data using argv. http://php.net/manual/en/reserved.variables.argv.php Sorry I am guessing because I don't know all your architecture, hope could help. – nik.longstone Mar 14 '15 at 13:49