H
H
hung2014-01-18 14:45:30
linux
hung, 2014-01-18 14:45:30

How to fork in PHP?

Good day to all.
The following problem arose: It is
necessary to implement the ability to run third-party programs through PHP. These programs are quite resource-intensive, therefore, they must be run in the background.
pcntl is not suitable as PHP should not be run via CGI.
The following idea came to mind: write a C program that takes as a parameter the path to the program to be executed, forks and runs it.
From PHP, this program is called by the exec function:

$cmd = "sudo -u aleksey run_script ".$a['path'];
exec($cmd);

run_script source:
#include <unistd.h>
int main(int argc, char *argv[])
{
  pid_t pid;
  pid = fork();
  if (pid == 0) {
    execl(argv[1], NULL);
  }
  else
    return 0;
}

It seems that PHP should not wait for the completion of the execution of the program, the path to which is passed to argv[1]. But in reality it's not like that. I might as well just call exec with the correct path.
Debian OS, apache+php5.4.4
Actually, the question is: what am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey, 2014-01-18
@hung

Judging by the description, you do not need a fork, but just a script run.
This construction will translate the script output to dev/null, so there will be no waiting.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question