Parallel execution of PHP - php

I have a php script file test1.php
<?php
echo "hello\n"
?>
When i try to run the php through command line using following code only one php process run and other php process is stopped.
We can see only one hello is printed.
Why other process is stopped ?
[root#home usr]# php -f test1.php & php -f test1.php
[30] 20817
hello
[root#home usr]#
[30]+ Stopped php -f test1.php
PS:
PHP 5.4.16 (cli) (built: Jun 23 2015 21:17:27)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies

The scripts get executed in parallel.
Try this modified version of a test script:
<?php
// Read first command line argument
$delay = (int) $argv[1];
// Wait for $delay seconds
sleep($delay);
// Output some message
echo "Result after $delay seconds\n";
If you invoke this script via php hello.php 3 & php hello.php 1 you will see that the first call will run for 3 seconds and print some output while the second call will only run for 1 second and print some output before the first call sends output:
root#httpserver:/tmp# php hello.php 3 & php hello.php 1
[1] 15991
Result after 1 seconds
root#httpserver:/tmp# Result after 3 seconds
If you are looking for other ways of parallel script execution you should have a look at this article https://d-mueller.de/blog/parallel-processing-in-php/

Related

Not able to print kubectl output on web browsers using php shell_exec() function

I'm not able to print kubectl output on chrome web browsers using php shell_exec() function.
i'm trying to display output of shell_exec() on my web browser.
The php code is working fine when i run on terminal but as i run on chrome then only output2 is printing. But i need to print output1 as well.
Can any one help to resolve this issue?
Running my status.php with below on CentOS Linux release 7.9.2009 (Core)
cmd: php status.php
My status.php page:
<?php
$ns = "bind";
echo $ns;
$cmd = "kubectl --kubeconfig /var/www/html/config get pod -n $ns";
$cmd2= "ls -lrth";
echo "<pre>";
$output1 = shell_exec($cmd);
$output2 = shell_exec($cmd2);
echo $output1; // Printing this output value on terminal but not printing on web browser
echo $output2;
echo "</pre>"; // Printing this output2 value on terminal as well web browser
echo "after shell_exce funtion"
?>
--
My OS is CentOS Linux release 7.9.2009 (Core)
cat /etc/redhat-release
my php version:
PHP 5.4.16 (cli) (built: Apr 1 2020 04:07:17)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
i'm trying to display output of shell_exec() on my web browser.
code is working fine when i run on terminal but as i run on chrome then only output2 is printing.
But i need to print output1 as well.

How to get iOS to run a sub process?

I am writing an app for iOS that will start the php binary as a sub process.
The following code is used:
NSString * command = #"/bin/sh -c '";
command = [command stringByAppendingString:[BibleditPaths php]];
command = [command stringByAppendingString:#" -v' > /tmp/php.txt 2>&1"];
int output = system ([command UTF8String]);
NSString* msg = [#(output) stringValue];
NSLog(msg, #"");
The php binary can be started on a jailbroken iPad from the command line. This is the output:
PHP 5.4.31 (cli) (built: Aug 19 2014 11:06:21)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies
When starting the php binary from the code above, iOS sends the SIGKILL signal.
The php binary does not run or produce any output.
/bin/sh produces this output:
sh: line 1: 2806 Killed: 9 /bin/sh -c '/var/mobile/Applications/DFE552CC-ECBE-4A73-82CF-24870E5D9F62/Library/usr/local/bin/php -v' > /tmp/php.txt 2>&1
2014-09-15 17:33:41.676 PHPRunner[2804:60b] 35072
What can I do to get iOS to run this process successfully?
This question was posted on the Apple iOS development forum (https://devforums.apple.com/index.jspa).
The response there was:
"None of that is supported (or permitted by the application sandbox) on iOS."
Subsequently the question was deleted from the forum.
I am posting the answer here so the information is kept for the public.

shell command aborts with php exec, but runs perfectly directly in shell (no memory issue)

I'm trying to execute a shell script via php exec:
gs -sDEVICE=png16m -dNOPAUSE -dGraphicsAlphaBits=4 -dDOINTERPOLATE -dTextAlphaBits=4 -sOutputFile=%03d.png -r300 ../mydpf.pdf -dFirstPage=2 -dBATCH -quit;
If i run this in the shell it finishes normally, but if I run this using php exec from web it stops after 16 pages are processed and gives no error. here is the output:
GPL Ghostscript 9.06 (2012-08-08)
Copyright (C) 2012 Artifex Software, Inc. All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
Processing pages 2 through 44.
Page 2
Page 3
Page 4
Page 5
Page 6
Page 7
Page 8
Page 9
Page 10
Page 11
Page 12
Page 13
Page 14
Page 15
Page 16
Page 17
In the PHP file i have:
ini_set('memory_limit', '1024M');
ini_set('max_execution_time', '18000');
So this is not an memory/execution_time issue.
Could You please help me - what could it be, that stops the script?
Thanks a lot.
Use shell_exec to be able to see what is being returned.
I had the same problem, this is my workaround, hope it helps:
Create a bash shell script in which you invoke the gs with your parameters, and then run the sh script with PHP exec().

Can't run bash script in PHP

I'm trying to run bash script in PHP but can't run it.
php -v
PHP 5.3.10-1ubuntu3.2 with Suhosin-Patch (cli) (built: Jun 13 2012 17:19:58)
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies
Ubuntu 12.04 LTS 64 bit.
My php code:
$cmd='/www/var/pl/bash.sh';
$retval =-1;
exec( $cmd, $output ); //executing the exec function.
foreach( $output as $tmp )
{
echo "$tmp <br>";
};
bash.sh:
#!/bin/bash
swipl --quiet -s /var/www/pl/ples.pl -g "f(R, gel), writeln(R),open('/var/www/pl/in.txt',write, Stream),
write(Stream, (R)),
nl(Stream),
close(Stream)" -t halt.
What am I doing wrong?
And I can run bash.sh in the Linux terminal.
When you run the script in the terminal you are executing it under the account you are logged in to. You have a shell setup with a search path etc.
When php executes the script, it has not a shell setup, and runs under the webserver user account. When executing:
make sure you have complete paths to your file, swipl is not enough, it should be /path/to/swipl
make sure the webserver process has enough access rights to get everything it needs.
Most likely it is either a path or permission problem; for example the user the web application runs as, has no idea where the swipl program is.
Add 2>&1 to the command line before exec'ing it, so that it tells you what the problem is. Or you can find the stderr output into the web server error log (or PHP error log; check its path and settings in php.ini).

PHP script won't run in the background

I am trying to run a php CLI script in the background and it just won't run - it has a status of Stopped SIGTOU (Trying to write output) - Here are the details
Mac OS X Lion 10.7.2
PHP 5.3.6 with Suhosin-Patch (cli) (built: Sep 8 2011 19:34:00)
I created a basic script test.php
<?php echo 'Hello world'.PHP_EOL; ?>
Here are the results of various tests:-
php -f test.php (Hello world gets displayed)
php -f test.php >test.log 2>&1 (Hello world gets put into test.log)
php -f test.php >test.log 2>&1 & --- I get [1]+ Stopped(SIGTTOU) php -f test.php > test.log 2>&1 -- and the job just sits there doing nothing nothing gets logged however lsof shows the log file is open
It is something to do with PHP? A similar shell script gets executed no problems in the background.
If readline is enabled in your build of php, simply pass /dev/null as the input.
In your example above, it would be:
php -f test.php </dev/null >test.log 2>&1
This is resolved now -- thanks to all who responded. The problem was that Apple provide PHP pre-built with the OS - the CLI version was built with readline included - http://www.php.net/manual/en/intro.readline.php ... this prevents any background running of scripts because readline automatically starts IO with the TTY ...
My problem was that I couldn't build my own version of PHP because of this -> http://forums.macrumors.com/showthread.php?t=1284479 - once I got that resolved my background script issue was gone :)
Well, a PHP script stops when its done execution, ie a simple echo "Hello World" is done execution as soon as it outputted the string, i would guess it have something to do with it ;-)

Categories