At the moment I have a line of code like this:
system("/usr/bin/php myphpscript.php --param=".val);
Is there a way to make php not wait for the script to finish - and just move on instead?
It's a loop moving email, and the myphpscript.php is parsing the mails. And I don't wan't to wait for myphpscript.php to finish each time - just start it and move on!
UPDATE SOLUTION
Found the answer here:
http://www.php.net/manual/en/function.exec.php#101506
passthru("/usr/bin/php myphpscript.php --param=".val." >> /dev/null 2>&1 &");
Exp:
/dev/null
I needed to write to something else that STDOUT, else PHP will hang untill script finish. So I write to /dev/null instead.
2>&1
Redirecting errors to STDOUT
&
"Run in background" as mentioned in this thread.
Have a good day!
jack
jack
This should work
exec("/usr/bin/php myphpscript.php --param=".val . '&');
Throwing the background command to the end should le the script continue:
system("/usr/bin/php myphpscript.php --param=".val . "&");
I'd also add nohup just to be safe, since I think this sub-process might get killed when the parent php script finishes:
system("nohup /usr/bin/php myphpscript.php --param=".val . "&");
Related
I have the following code on my index.php file. But it is not working properly.. When i directly visit domain.com/script.php it works. I need this script to be executed in the background while accessing index page. Can anyone help me?
shell_exec('php script.php > /dev/null 2>/dev/null &');
check php is running in safe mode or not shell_exec is disabled in safe mode for the sake of security why don't you use
curl
to run the code
Well I think there are 2 possible issues in your case
1) try this:
shell_exec("script.php 2>/dev/null >/dev/null &");
OR
shell_exec("script.php 2>&1 | tee -a /tmp/mylog 2>/dev/null >/dev/null &");
2) A simple way to handle the problem of capturing stderr output when using shell-exec under windows is to call ob_start() before the command and ob_end_clean() afterwards
ob_start();
ob_end_clean();
Well instead of using shell_exec, you can make an ajax call to script.php when the user visits index.php.
Another option is to run the script.php as a cron job every 5 minutes or so. When the user visits index.php, some data can be saved to database indicating that script.php should run. script.php should check if it is marked for running.
I would like to run a php script in the background as it takes much time and I don't want to wait for it.
This question relates to:
php execute a background process
I tried to make this work on my linux centos machine, but unfortunately it seems not to be working.
This is my code:
$cmd = 'php '.realpath($_SERVER["DOCUMENT_ROOT"]).'/test/sample.php';
exec(sprintf("%s > %s 2>&1 & echo $! >> %s", $cmd, $outputfile, $pidfile));
exec(sprintf("$s > $s 2>&1 & echo $1", $cmd, $outputfile),$pidArr);
sleep(2);
print_r($outputfile);
print_r($pidfile);
I included the sleep(2) to make sure the sample.php script is finished. The sample.php only contains echo 'Hello world'; I tried both exec options, but none of them worked. The above script doesn't show any output.
I tried to run the same $cmd in linux command line and it showed me the output from sample.php script.
I would like to make this run first, but in addition I would like to send variables to the sample.php script as well.
Your help is very appreciated.
You can use set_time_limit(0) to allow script to run indefinitely and ignore_user_abort() to keep running script in background, even if the browser of tab have been closed. This way if you redirect your browser to somewhere else, script will keep running in background.
This approach to running php scripts in the background seems a bit awkward. And depending on if you pass additional parameters based on user input, could also create a scenario where an attacker could inject additional commands.
For use cases like this, you could be using cron, if you don't need a response immediately, or a job manager like Gearman, where you can use a Gearman server to manage the communication between the we request and the background job.
I made some adjustments
$cmd = 'php '.realpath($_SERVER["DOCUMENT_ROOT"]).'/test/sample.php';
$outputfile = 'ouput.txt';
$pidfile = 'pid.txt';
exec(sprintf("%s > %s 2>&1 & echo $! >> %s", $cmd, $outputfile, $pidfile));
exec(sprintf("$s > $s 2>&1 & echo $1", $cmd, $outputfile),$pidArr);
sleep(2);
print_r(file_get_contents($outputfile));
print_r(file_get_contents($pidfile));
Not sure if you defined the variables before. I tested it on Windows and it works
I' am calling sell script from my php code
with
foreach ($some_array) {
shell_exec(nohup $code);
}
like above
I want all shell_exec call to work independent from mail process which is php execution that we call shell script
But It's not working as I expected all shell_executions start right after previous one completed
So how can I make this shell_exec calls as independent child process that they don't wait each others completation
Thanks in advance
Add the '&' to the end of the command you want to execute so it works in background.
For a sequence of commands, enclose them within parentheses then append the & symbol but be sure to redirect stdout, stderr somewhere otherwise your script will hang waiting e.g.:
<?php
exec('( sleep 10; echo "finished" | mail ian#example.com ) &> /dev/null &');
?>
See http://us.php.net/manual/en/function.exec.php
Send them to the background
shell_exec("nohup somecommand &");
^---run job in background
$escaped_check = escapeshellcmd("/usr/bin/php -f /opt/status/check.php " . $_SERVER['REMOTE_ADDR'] . " >> /dev/null 2>&1 &");
shell_exec($escaped_check);
I am trying to execute the above code in the background for the sake of non-blocking thread, but I am not sure how to exit the check.php when the job is done.
you can popen instead of shell_exec.
Then you can terminate it by proc_terminate
Or you can obtain process id by calling proc_get_status.
With PID you can terminate process also.
I hope this helps.
The PHP script (your check.php) should be terminated automatically after it finishes running at background.
You probably can try using exit() or die() at the end of the PHP script (your check.php) to make sure the script does jump out .
You can also use the following command to get the $pid. For example, 23456. The "echo $!" part sends the process id.
$pid = shell_exec('php /path/to/script/cli_test.php argument1 > /dev/null 2>&1 & echo $!')
And then in the terminal, use ps command to check if it is still running at background.
ps 23456
I need to run a command in PHP like this:
exec('dosomething > saveit.txt');
Except I don't want PHP to wait for it to be complete. I also don't want to throw away the output, and I don't want to use nohup because I'm using that for something else in the same directory.
I also tried pclose(popen('dosomething > saveit.txt','r')); and that didn't work, it still waited.
Add an ampersand to the end of the command, so:
exec('dosomething > saveit.txt &');
in the documentation of exec() there is an interesting comment that says:
Took quite some time to figure out the line I am going to post next. If you want to execute a command in the background without having the script waiting for the result, you can do the following:
<?php
passthru("/usr/bin/php /path/to/script.php ".$argv_parameter." >> /path/to/log_file.log 2>&1 &");
?>