PHP - run script in background - php

I have to execute a php script (test.php) in the background. I tried this but it's not working:
<?
$cmd = "php /home/megad404/www/prove/test.php &> /dev/null &";
exec('/bin/bash -c "'.$cmd.'"',$output,$return);
if($return===0)
{
echo 'Successful';
}
else
{
echo 'Unsuccessful';
}
?>
It returns "Successful" but it doesn't execute test.php
test.php:
<?
file_put_contents(date("s"),"");
sleep(5);
file_put_contents(date("s"),"");
sleep(5);
file_put_contents(date("s"),"");
?>
test.php writes a file every 5 second and it works fine, except if I try to execute it in the background with the first script.
Could it be a server issue? Is there another way to run a script in the background?

Use shell_exec and nohup
shell_exec("nohup php /home/megad404/www/prove/test.php > /dev/null & echo $!");

Use shell_exec and give absolute path for php:
$output = shell_exec("nohup /usr/bin/php7.0 -f /home/megad404/www/prove/test.php &> /dev/null &");
Just confirm the absolute path for php in your server. For instance, I'm using php 7.0 and the absolute path is /usr/bin/php7.0
Also, give executable permission to the php file you are running from your code.
chmod +x /home/megad404/www/prove/test.php

Related

exec() error, i cant figure it out how to make it work

I'm trying to run a script in the background but it's not working, this is the script I'm running:
$ddd = "script.php?titulo=".$_REQUEST['titulo']."&descripcion=".$_REQUEST['descripcion']."&accion=".$encrypta->encode('comunicadoPadres')."";
exec("wget -qO- $ddd &> /dev/null &");
If I run the url directly on the browser the script works but from this exec() nothing happens
wegt command only access path location, you can't compile&run php script if you want use wget then use HTTP path of script
$ddd = "http://domain.com/script.php?titulo=".$_REQUEST['titulo']."&descripcion=".$_REQUEST['descripcion']."&accion=".$encrypta->encode('comunicadoPadres')."";
exec("wget -qO- $ddd &> /dev/null &");

PHP exec and header redirect

I have a long running PHP script that i want to be executed in background on server after a user action. and the user should be redirected to other page while command should be running in background.
Below is the code
$command = exec('php -q /mylongrunningscript.php');
header("Location: /main.php?action=welcome");
The above script is running fine, but page does not redirected until $command = exec('php -q /mylongrunningscript.php'); is executed.
I want that user should be immediately redirected to the welcome page.
Is there any other way to achieve this task.
The other idea is that that $command = exec('php -q /mylongrunningscript.php'); should be executed on welcome page, but welcome page HTML is shown after the command has executed. command takes about 5,6 minutes and this time page does not redirects.
I am On Cent os Linux with PHP 5.3
Can you try this instead:
$result = shell_exec('php -q /mylongrunningscript.php > /dev/null 2>&1 &');
PS: Note that this is redirecting stdout and stderr to /dev/null If you want to capture output then use:
$result = shell_exec('php -q /mylongrunningscript.php > /tmp/script.our 2>&1 &');
Alternatively use this PHP function to run any Unix command in background:
//Run linux command in background and return the PID created by the OS
function run_in_background($Command, $Priority = 0) {
if($Priority)
$PID = shell_exec("nohup nice -n $Priority $Command > /dev/null & echo $!");
else
$PID = shell_exec("nohup $Command > /dev/null & echo $!");
return($PID);
}
Courtesy: A comment posted on http://php.net/manual/en/function.shell-exec.php
As noted in the exec() manual page of PHP:
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.
So let's do that, using 2>&1 (basically 2 is stderr and 1 is stdout, so what this means is "redirect all stderr messages to stdout"):
shell_exec('php -q /mylongrunningscript.php 2>&1');
or if you want to know what it outputs:
shell_exec('php -q /mylongrunningscript.php 2>&1 > output.log');
Send the script output to /dev/null and the exec function will return immediately
$command = exec('php -q /mylongrunningscript.php > /dev/null 2>&1');

Php Exec timeout

I have the following exec() command with an & sign at the end so the script runs in the background. However the script is not running in the background. It's timing out in the browser after exactly 5.6 minutes. Also if i close the browser the script doesn't keep running.
exec("/usr/local/bin/php -q /home/user/somefile.php &")
If I run the script via the command line, it does not time out. My question is how do i prevent timeout. How do i run the script in the background using exec so it's not browser dependent. What am i doing wrong and what should i look at.
exec() function handle outputs from your executed program, so I suggest you to redirect outputs to /dev/null (a virtual writable file, that automatically loose every data you write in).
Try to run :
exec("/usr/local/bin/php -q /home/gooffers/somefile.php > /dev/null 2>&1 &");
Note : 2>&1 redirects error output to standard output, and > /dev/null redirects standard output to that virtual file.
If you have still difficulties, you can create a script that just execute other scripts. exec() follows a process when it is doing a task, but releases when the task is finished. if the executed script just executes another one, the task is very quick and exec is released the same way.
Let's see an implementation. Create a exec.php that contains :
<?php
if (count($argv) == 1)
{
die('You must give a script to exec...');
}
array_shift($argv);
$cmd = '/usr/local/bin/php -q';
foreach ($argv as $arg)
{
$cmd .= " " . escapeshellarg($arg);
}
exec("{$cmd} > /dev/null 2>&1 &");
?>
Now, run the following command :
exec("/usr/local/bin/php -q exec.php /home/gooffers/somefile.php > /dev/null 2>&1 &");
If you have arguments, you can give them too :
exec("/usr/local/bin/php -q exec.php /home/gooffers/somefile.php x y z > /dev/null 2>&1 &");
You'll need to use shell_exec() instead:
shell_exec("/usr/local/bin/php -q /home/gooffers/somefile.php &");
That being said, if you have shell access, why don't you install this as a cronjob? I'm not sure why a PHP script is invoking another to run like this.

Running PHP script in background

I am testing a php script that has been developed on an OS-X system at Debian and it behaves different there.
To reproduce it I wrote two scripts: parent.php and child.php:
parent.php:
#!/usr/bin/php
<?php
echo "parent started...\n";
shell_exec(__DIR__ . '/child.php &2>/dev/null &');
echo "parent finished.\n";
child.php:
#!/usr/bin/php
<?php
echo "child started...\n";
sleep(5);
echo "child finished.\n";
Running parent.php on OS-X I get back imediately the two output lines (parent started, parent finished). On Debian I get the "parent started..." line, then a delay of 5 seconds an then the "parent finished.". Running "./child.php &2>/dev/null &" in the shell gives me back the prompt imediately as expected. Any ideas how I can fix this?
This is because &2> part. It may not be supported in all systems. Also in every shell (bash, sh, ksh etc).
Try this,
exec("/bin/bash -c '/usr/bin/php /path/to/child.php 2> /dev/null' &");
If you want to suppress all the output use this,
exec("/bin/bash -c '/usr/bin/php /path/to/child.php &> /dev/null ' &");
BASH-HOWTO
Just tested, exec("/usr/bin/php /path/to/child.php > /dev/null 2>&1 &") should work too.
Try with exec() or system() instead of shell_exec, maybe shell_exec has not the same behavior on different OS.

Shell_exec php with nohup

I think there are tons of similar posts but I haven't yet found a solution after searching around.
Basically, I'm trying to run two scripts in the background. When I run them in the commandline, I see after calling my first script:
/usr/bin/nohup php script.php > nohupoutput.log & echo $!
I've tried ...script.php > /dev/null & with the same result. I get:
/usr/bin/nohup: ignoring input and redirecting stderr to stdout
which I ignore and run the second one. I noticed that it seemed to be hanging there, and pressing Enter brought me back to machine:~folder>
/usr/bin/nohup php script2.php > nohupoutput.log & echo $!
Both scripts work. I tried to then convert this to a shell_exec command and nothing seems to work. I suspect that the ignoring input bit is causing difficulties, but I'm not sure. Regardless, the following does not work. It just hangs in the browser:
$output = shell_exec('/usr/bin/nohup php script.php > /dev/null &');
$output = shell_exec('/usr/bin/nohup php script2.php > /dev/null &');
Try:
$output = shell_exec('/usr/bin/nohup php script.php >/dev/null 2>&1 &');
Or:
exec('/usr/bin/nohup php script.php >/dev/null 2>&1 &');
This shoul work:
shell_exec('nohup /usr/bin/php path/to/script.php > output.txt &');
<?php
function execInBackground($cmd) {
if (substr(php_uname(), 0, 7) == "Windows"){
pclose(popen("start /B ". $cmd, "r"));
}
else {
exec($cmd . " > /dev/null &");
}
}
// take note: to get your PHP_PATH, try looking at your phpinfo :)
echo execInBackground("/usr/local/php53/bin/php 'example2.php'");
?>
First put your php command in a shell file script, e.g. myscript.sh:
#!/bin/bash
# myscript.sh file
php script.php
Run nohup with myscript.sh:
sudo nohup ./myscript.sh &
Verify with ps:
ps aux | grep myscript.sh

Categories