PHP Background Processes

php execute a background process

Assuming this is running on a Linux machine, I've always handled it like this:

exec(sprintf("%s > %s 2>&1 & echo $! >> %s", $cmd, $outputfile, $pidfile));

This launches the command $cmd, redirects the command output to $outputfile, and writes the process id to $pidfile.

That lets you easily monitor what the process is doing and if it's still running.

function isRunning($pid){
try{
$result = shell_exec(sprintf("ps %d", $pid));
if( count(preg_split("/\n/", $result)) > 2){
return true;
}
}catch(Exception $e){}

return false;
}

PHP Background Processes

Well, you can use "ignore_user_abort(true)"

So the script will continue to work (keep an eye on script duration, perhaps add "set_time_limit(0)")

But a warning here: You will not be able to stop a script with these two lines:

ignore_user_abort(true); 
set_time_limit(0);

Except you can directly access the server and kill the process there! (Been there, done an endless loop, calling itself over and over again, made the server come to a screeching stop, got shouted at...)

php exec() background process issues

Note:

If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.

http://php.net/manual/en/function.exec.php

so:

exec("php csv.php $file $user > /dev/null &"); // no $output

PHP - Long Running Background Task

You want to be careful with long-running PHP processes as, for one PHP is not very memory efficient (as an example an array of just 100 ints in PHP can consume as much as 15KB of memory). This is normally fine for 99% of the use cases since most people are just using PHP for websites and those processes run for fractions of a second so memory is sacrificed for speed. However, for a long-running process (especially if you have a lot of them) this may not be your best solution.

You also want to be very careful calling exec/shell_exec like functions in PHP as they are internally implemented as streams (i.e. they can cause blocking in the parent process as it normally has to wait on the stream to return data).

One option to background the task is to use fork. However, I strongly suggest using a proper job manager like gearman (see php extensions also), or queue, like amqp or zmq, to handle these tasks more cleanly. Which one is more suitable for your use case, I'll let you decide.

How to create background process in PHP

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!



Related Topics



Leave a reply



Submit