Run Multiple PHP files using PHP Command Line or SSH - php

I have 137 php files i want to run them in one command (in parallel) not by sequence.
But the problem is each file is taking 2-5 seconds.
So i have tried to make a (.sh) file and put each line as :
/usr/bin/php /files/file1.php
/usr/bin/php /files/file2.php
/usr/bin/php /files/file3.php
It will complete file1 and then run file2 and file3 by sequence.
So please what is the php or sh command to run 137 php files all in one click (parallel).

You put them in background.
for ($i=1; $i<=137; $i++) {
exec("/usr/bin/php /files/file$i.php > /dev/null 2>&1 &");
}

Run the scripts in the background by adding 'nohup' and '&'
nohup /usr/bin/php /files/file1.php &
nohup /usr/bin/php /files/file2.php &
nohup /usr/bin/php /files/file3.php &

You can use pcntl lib , that enables threads in php, you can use that and create a php-master file that will call other , and then you can master file from command file

Related

PHP Multi Statement Exec in Background

I have a PHP script that needs to run some background command line calls to create some files in the background. I do not want the user to have to wait until the command line stuff is done before the page loads. I have tried using the answers from other threads on SO and it is not working for me.
Here is what I have tried via exec() and shell_exec() to get it to run in the background:
'{ [multiple commands here] } &> /dev/null &;
If it is helpful, this is the server info:
uname -or
2.6.39-400.17.1.el6uek.x86_64 GNU/Linux
lsb_release -irc
Distributor ID: OracleServer
Release: 6.4
Codename: n/a
Maybe this example will help you out. Replace the commands with your commands.
<?php
shell_exec("{ sleep 10; touch /tmp/asdf; } > /dev/null 2>&1 &");

shell_exec wont work from crontab

I have looked at other answers they dont fit to this case.
I am using the full path to the file. Code I copied is simplified.
run.php contains:
shell_exec("php /var/www/html/sync/chourly.php $position $quotientx > /dev/null 2>/dev/null &");
if I use manually php run.php - it works great.
here is the line on crontab -e :
05 * * * * /usr/bin/wget -O /dev/null http://sync.eeeww.com/run.php
again the file run.php starts BUT chourly.php doesn't start. I am using centOS 6
any suggestions please?
Addition: I checked the permissions I am using ec2-user to run php run.php and crontab is using the same permission. it is able to run the file but shell_exec is where the issue occurs
Is /var/www/html/sync/chourly.php using $SERVER['DOCUMENT_ROOT'] ? Since you're explicitly calling the php interpreter (not mod_php), a `$SERVER['DOCUMENT_ROOT'] call will not work as you expect.
Try manually running the cron from shell to see where it's failing.
cd /
su - your_httpd_usersame -c "/usr/bin/wget -O /dev/null http://sync.bitpine.com/run.php"

Run multiple php scripts in sequence from a single php script or batch file in windows

I have 5 php scripts need to be executed one after other. How can i create a single php/batch script which executes all the php scripts in sequence.
I want to schedule a cron which run that php file.
#!/path/to/your/php
<?php
include("script1.php");
include("script2.php");
include("script3.php");
//...
?>
Alternative
#/bin/bash
/path/to/your/php /path/to/your/script1.php
/path/to/your/php /path/to/your/script2.php
/path/to/your/php /path/to/your/script3.php
# ...
If your scripts need to be accessed via http:
#/bin/bash
wget --quiet http://path/to/your/script1.php > /dev/null 2>&1
wget --quiet http://path/to/your/script2.php > /dev/null 2>&1
wget --quiet http://path/to/your/script3.php > /dev/null 2>&1
# ...
I did something for testing using the "wait" command, as it seems I just put wait between the calls to each script. I did a little test creating databases in php scripts, returning some records in a bash script, then updating with another php script, then another bash script to return the updated results and it seemed to work fine...
From what I have read, as long as the subscript doesn't call another subscript, the master script will wait if "wait" command is used between script calls.
Code is as below:
#!/bin/sh
/usr/bin/php test.php
wait
/bin/bash test.sh
wait
/usr/bin/php test2.php
wait
/bin/bash test2.sh
wait
echo all done
Hope it would execute all the php scripts in sequence.

Exec to launch php as background process

I am struggling with getting a php file to run in the background with PHP's exec(). As a first test, I tried :
exec("ls -l > logfile.txt 2> errfile.txt &");
That works fine. logfile.txt gets filled with a directory listing.
Per instructions in the php documentation, since the exec kicks off a process that runs in the background, standard out (and standard error) are redirected to a file.
Now, I try
exec("/usr/bin/php -f /path/to/my.php > logfile.txt 2> errorfile.txt &");
It appears nothing happens.
Here are test files that I'm trying:
alpha.php
<?php
$version="a";
// Go do something we do not need to wait for.
exec("/usr/bin/php -f /path/to/beta.php > logfile.txt 2> errorfile.txt &");
?>
<html>
<head><title>Test</title></head>
<body>
<p>This is Alpha version <?php echo $version; ?></p>
</body>
</html>
beta.php
<?php
if (!($fp = fopen('/home/johnst12/public_html/workshops/admin/betadata.txt', 'w'))) { exit;}
fprintf($fp, "Proof that Beta executed.");
fclose($fp);
?>
If I run beta.php directly, it works fine. Betadata.txt gets the message.
If I run alpha.php to launch beta.php, betadata.txt is not created. logfile.txt and errorfile.txt remain empty (expected).
I am sure that the path to php, and the path to my php file are correct.
Googling for clarification has not been fruitful. A couple of common themes seem to be (a) running out of resources? (b) lack of permission on the target php file? Out of resources seems unlikely. The permission on the script is global read 644 (rw-r--r--). I tried adding execute (755) just in case it would help. It made no difference.
PHP version 5.3.21
Linux/Apache system.
safe_mode Off
What am I missing? Thanks.
First of all : Have you verified that /usr/bin/php is the correct path to PHP?
Php doesn't like running like that. Something to do with stdin. Try with nohup:
exec("nohup /usr/bin/php -f /path/to/beta.php > logfile.txt 2> errorfile.txt &");
With -f anything else that looks like a flag will go to PHP, so if you wanted to pass a "-x" option to your script then you'd have to
/usr/bin/php -f /path/to/beta.php -- -x
Without, options before the filename go to PHP and after go to the script.
/usr/bin/php /path/to/beta.php -x
I assume you've already looked at the two files in case they have output or errors?
A few other things to check:
Delete the two files. Are they recreated each time this code runs?
exec("nohup /usr/bin/php -v > logfile.txt &");
should output version information to that log file.
exec("/usr/bin/php -f /path/to/beta.php > logfile.txt 2> errorfile.txt");
should run the script properly (but not in the background).

How to execute a makefile in a php file

Is there any way to execute a makefile in a php file? I have tried:
exec('cmd /c "C:\\Program Files\\Microsoft Visual Studio\\VC98\\Bin\\nmake.exe" -f E:\\dev\\temp.mak > process.out 2> process.err < /dev/null &');
But I donot think this way makefile gets to run.
Why not doing :
$make = escapeshellarg("C:\Program Files\Microsoft Visual Studio\VC98\Bin\nmake.exe");
$path = escapeshellarg("E:\dev\temp.mak");
exec("start /B {$make} -f {$path} > process.out 2> process.err");
start /B will execute your program in background
> process.out will redirect standard output to "process.out" file
2> process.err will redirect error output to "process.err" file
In this example, process.out and process.err will be erased each time make is run. To avoid this behaviour, just replace > symbols by >>, and files will be appended.
Try different methods of doing this, Create a windows batch file in the same place as your PHP directory;
cd C:\"Program Files"\"Microsoft Visual Studio"\VC98\Bin
nmake.exe -f E:\dev\temp.mak > process.out 2> process.err
*Incorporating Zids Comment into this: *
How would you execute it from the command line, if you were not using PHP? – rid
If the above method doesn't work. Search online for usage of that exe by running it from windows command prompt, then change the .batch file accordingly.*
Save this as a .batch file, then from your PHP try running
exec ("filename.bat", $output);
then
View the output performed by the exec command in a simple foreach loop
foreach ($output AS $OutputStr)
{
echo $OutputStr."<br>";
}
There should be some output, from the output I would work with that.

Categories