$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
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 have a process intensive task that I would like to run in the background.
The user clicks on a page, the PHP script runs, and finally, based on some conditions, if required, then it has to run a shell script, E.G.:
shell_exec('php measurePerformance.php 47 844 email#yahoo.com');
Currently I use shell_exec, but this requires the script to wait for an output. Is there any way to execute the command I want without waiting for it to complete?
How about adding.
"> /dev/null 2>/dev/null &"
shell_exec('php measurePerformance.php 47 844 email#yahoo.com > /dev/null 2>/dev/null &');
Note this also gets rid of the stdio and stderr.
This will execute a command and disconnect from the running process. Of course, it can be any command you want. But for a test, you can create a php file with a sleep(20) command it.
exec("nohup /usr/bin/php -f sleep.php > /dev/null 2>&1 &");
You can also give your output back to the client instantly and continue processing your PHP code afterwards.
This is the method I am using for long-waiting Ajax calls which would not have any effect on client side:
ob_end_clean();
ignore_user_abort();
ob_start();
header("Connection: close");
echo json_encode($out);
header("Content-Length: " . ob_get_length());
ob_end_flush();
flush();
// execute your command here. client will not wait for response, it already has one above.
You can find the detailed explanation here: http://oytun.co/response-now-process-later
On Windows 2003, to call another script without waiting, I used this:
$commandString = "start /b c:\\php\\php.EXE C:\\Inetpub\\wwwroot\\mysite.com\\phpforktest.php --passmsg=$testmsg";
pclose(popen($commandString, 'r'));
This only works AFTER giving changing permissions on cmd.exe - add Read and Execute for IUSR_YOURMACHINE (I also set write to Deny).
Use PHP's popen command, e.g.:
pclose(popen("start c:\wamp\bin\php.exe c:\wamp\www\script.php","r"));
This will create a child process and the script will excute in the background without waiting for output.
Sure, for windows you can use:
$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run("C:/path/to/php-win.exe -f C:/path/to/script.php", 0, false);
Note:
If you get a COM error, add the extension to your php.ini and restart apache:
[COM_DOT_NET]
extension=php_com_dotnet.dll
If it's off of a web page, I recommend generating a signal of some kind (dropping a file in a directory, perhaps) and having a cron job pick up the work that needs to be done. Otherwise, we're likely to get into the territory of using pcntl_fork() and exec() from inside an Apache process, and that's just bad mojo.
That will work but you will have to be careful not to overload your server because it will create a new process every time you call this function which will run in background. If only one concurrent call at the same time then this workaround will do the job.
If not then I would advice to run a message queue like for instance beanstalkd/gearman/amazon sqs.
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
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 . "&");
I have a process intensive task that I would like to run in the background.
The user clicks on a page, the PHP script runs, and finally, based on some conditions, if required, then it has to run a shell script, E.G.:
shell_exec('php measurePerformance.php 47 844 email#yahoo.com');
Currently I use shell_exec, but this requires the script to wait for an output. Is there any way to execute the command I want without waiting for it to complete?
How about adding.
"> /dev/null 2>/dev/null &"
shell_exec('php measurePerformance.php 47 844 email#yahoo.com > /dev/null 2>/dev/null &');
Note this also gets rid of the stdio and stderr.
This will execute a command and disconnect from the running process. Of course, it can be any command you want. But for a test, you can create a php file with a sleep(20) command it.
exec("nohup /usr/bin/php -f sleep.php > /dev/null 2>&1 &");
You can also give your output back to the client instantly and continue processing your PHP code afterwards.
This is the method I am using for long-waiting Ajax calls which would not have any effect on client side:
ob_end_clean();
ignore_user_abort();
ob_start();
header("Connection: close");
echo json_encode($out);
header("Content-Length: " . ob_get_length());
ob_end_flush();
flush();
// execute your command here. client will not wait for response, it already has one above.
You can find the detailed explanation here: http://oytun.co/response-now-process-later
On Windows 2003, to call another script without waiting, I used this:
$commandString = "start /b c:\\php\\php.EXE C:\\Inetpub\\wwwroot\\mysite.com\\phpforktest.php --passmsg=$testmsg";
pclose(popen($commandString, 'r'));
This only works AFTER giving changing permissions on cmd.exe - add Read and Execute for IUSR_YOURMACHINE (I also set write to Deny).
Use PHP's popen command, e.g.:
pclose(popen("start c:\wamp\bin\php.exe c:\wamp\www\script.php","r"));
This will create a child process and the script will excute in the background without waiting for output.
Sure, for windows you can use:
$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run("C:/path/to/php-win.exe -f C:/path/to/script.php", 0, false);
Note:
If you get a COM error, add the extension to your php.ini and restart apache:
[COM_DOT_NET]
extension=php_com_dotnet.dll
If it's off of a web page, I recommend generating a signal of some kind (dropping a file in a directory, perhaps) and having a cron job pick up the work that needs to be done. Otherwise, we're likely to get into the territory of using pcntl_fork() and exec() from inside an Apache process, and that's just bad mojo.
That will work but you will have to be careful not to overload your server because it will create a new process every time you call this function which will run in background. If only one concurrent call at the same time then this workaround will do the job.
If not then I would advice to run a message queue like for instance beanstalkd/gearman/amazon sqs.