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.
Related
I'm calling exec to run scripts in backgroud using php exec() and wget. But everytime I call this it's creating a file with url parameters in my server. I tried to disable output and tried to store it to a specific file also. Both failed.
here is my code
function execInBackground($cmd)
{
$cmd = escapeshellcmd($cmd);
exec($cmd . " > /dev/null &");
}
And I'm calling it like below
execInBackground('wget -q http://example.com/d/do-processing?process=501&res=201');
So now it will create a file named
do-processing?process=501&res=201
in my server. Can you guys please mention is there anything I missed while doing this.
I want to call that url in background without saving any output. In worst case I need to store the output of each call to a specific file instead of creating multiple files.
exec($cmd . " > process.out 2> process.err < /dev/null &");
I tried like above code and didn't worked.
The solution is to use wgets' -o or --output-file argument like this:
exec('wget -q -o /dev/null http://example.com/d/do-processing?process=501&res=201 &');
Then no output file will be created.
I am trying to execute a command with exec() and redirecting stdout and stderr to a file.
exec("nohup python main.py -i 1 > /var/scripts/logs/1_out.log 2>&1 &");
It will create the file but it will not print anything to it.
If I run the command in a terminal everything outputs without a problem.
Got it working. Python does its own output buffering which kept it from writing to the file. Running it with the -u option disables this. Final code looks like this:
exec("nohup python -u main.py -i 1 > /var/scripts/logs/1_out.log 2>&1 </dev/null &");
Thanks.
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).
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.
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