php in background exec() function
exec()
will block until the process you're exec'ing has completed - in otherwords, you're basically running your 'test.php' as a subroutine. At bare minimum you need to add a &
to the command line arguments, which would put that exec()'d process into the background:
exec("php test.php {$test['id']} &");
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
running php exec command in background
I will assume your running this on a *nix platform. To get php to run something in the background and not wait for the process to finish I would recommend 2 things: First use nohup
and also redirect the output of the command to /dev/null
(trash).
Example:
<?php
exec('nohup sar -u 1 > /dev/null 2>/dev/null &');
nohup means we do not send the "hang up" signal (which kills the process) when the terminal running the command closes.
> /dev/null 2>/dev/null &
redirects the "normal" and "error" outputs to the blackhole /dev/null
location. This allows PHP to not have to wait for the outputs of the command being called.
On another note, if you are using PHP just to call a shell command, you may want to consider other options like Ubuntu's Upstart with no PHP component--if you are using Ubuntu that is.
Execute a function in the background
You need to set a cron job to run your code every X minute/hour/day/...
Create a method in a controller and put your code inside it. Then set your cron job to run it at a special moments.
If you have cPanel or other management panels it would be easy.
Edit #1: To run in CLI you should run index.php using php command and send controller and action name as parameters. If you have a controller named User and a method named "happy_birthday", then you need to run this command: php index.php users happy_birthday
.
php background process using exec function
You have to reroute programs output somewhere too, usually /dev/null
exec($cmd . " > /dev/null &");
Running a PHP exec() in the background on Windows?
Include a redirection in the command line:
exec('program.exe > NUL')
or you could modify your program to explicitly close standard output, in C this would be
CloseHandle(GetStdHandle(STD_OUTPUT_HANDLE));
It is possible (the documentation doesn't say) that you might need to redirect/close both the standard output and standard error:
exec('program.exe > NUL 2> NUL')
or
CloseHandle(GetStdHandle(STD_OUTPUT_HANDLE));
CloseHandle(GetStdHandle(STD_ERROR_HANDLE));
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;
}
Can exec() php function run programs not in the background?
You can't because php doesn't have access to the GUI.
End the background process of php exec command
To launch your background process, use a command like this:
exec('nohup java jar "/selenium-server-standalone-3.8.0.jar" > /dev/null 2>&1 & echo $!', $pid);
This is redirecting stdout to /dev/null, and then stderr to stdout so that the process can actually continue in the background. After that, it is returning the processid of the new background process and saving it in $pid.
Then, later, you can stop it with exec('kill '.$pid);
Related Topics
Good Error Handling with File_Get_Contents
How to Execute a Stored Procedure in PHP Using SQLsrv and "" Style Parameters
Why Is Calling a Function (Such as Strlen, Count etc) on a Referenced Value So Slow
JavaScript Function Post and Call PHP Script
Php: iPad Does Not Play Mp4 Videos Delivered by PHP, But If Accessed Directly It Does
How to Add Namespace to an Attribute with PHP's Simplexml
Fix Incorrectly Displayed Encoding on an HTML Document with PHP
How to Get User Timezone Using Jquery
Use Git Smudge/Clean to Replace File Contents
How to Make a Cascading Drop Down List in PHP Using Jquery
How to Download Large Files Through PHP Script