I'm developing a php function for execute a command that's build gradle project Android.
But to be clear I want to create a small system can modify a android project source and generate the APK project by build the gradle.
I do a lot of search about it and I found some close question but not with this command.
This is my function. It's really simple, I have already installed gradle tools in my server, and the command runs successfully in my terminal
function generator(){
exec('cd /home/my/AndroidStudioProjects/BetaProject/;./gradlew assembleDebug 2>&1',$out,$err);
var_dump($out);
var_dump($err);
}
But, when I execute it with php it's gives me this :
string(955) "Exception in thread "main" java.lang.RuntimeException:
java.io.FileNotFoundException:
/usr/sbin/.gradle/wrapper/dists/gradle-2.4-all/3i2gobhdl0fm2tosnn15g540i0/gradle-2.4-all.zip.lck
(No such file or directory) at
org.gradle.wrapper.ExclusiveFileAccessManager.access(ExclusiveFileAccessManager.java:78)
at org.gradle.wrapper.Install.createDist(Install.java:47) at
org.gradle.wrapper.WrapperExecutor.execute(WrapperExecutor.java:129)
at
org.gradle.wrapper.GradleWrapperMain.main(GradleWrapperMain.java:48)
Caused by: java.io.FileNotFoundException:
/usr/sbin/.gradle/wrapper/dists/gradle-2.4-all/3i2gobhdl0fm2tosnn15g540i0/gradle-2.4-all.zip.lck
(No such file or directory) at java.io.RandomAccessFile.open0(Native
Method) at java.io.RandomAccessFile.open(RandomAccessFile.java:316) at
java.io.RandomAccessFile.(RandomAccessFile.java:243) at
org.gradle.wrapper.ExclusiveFileAccessManager.access(ExclusiveFileAccessManager.java:49)
... 3 more "
If there's any solution or some advice, I will be so glad.
Thanks
try this:
function generator(){
exec('sh /home/my/AndroidStudioProjects/BetaProject/gradlew assembleDebug 2>&1',$out,$err);
var_dump($out);
var_dump($err);
}
I was facing the same issue, and I fixed it just by giving apache permission to execute these scripts by changing the owner of the directory where these files reside
It's going to be something like this:
sudo chown -R www-data your-directory/
I hope this is going to work for you.
Related
I have the following PHP code that is run when a user clicks a button on a local intranet page to run some Node JS commands. e.g.
exec('npm install', $output);
$output = implode(PHP_EOL, $output);
echo $output;
But the command doesn't seem to get run... It does if I type the command into the command prompt and run it...
I don't see any errors and NodeJS is set as a system variable in the Path so it should know what grunt is... Any ideas? Other commands, e.g. whoami run fine.
Any ideas?
I'm on Windows 7.
Update: Based on the comments below I can now get an error with:
exec('npm install 2>&1', $output, $returnCode);
$output = implode(PHP_EOL, $output);
echo $output . ' ' . $returnCode;
And this is the error:
TypeError: Cannot call method 'get' of undefined at C:\Program Files (x86)\nodejs\node_modules\npm\lib\npm.js:310:23 at C:\Program Files (x86)\nodejs\node_modules\npm\node_modules\npmconf\npmconf.js:80:7 at Array.forEach (native) at C:\Program Files (x86)\nodejs\node_modules\npm\node_modules\npmconf\npmconf.js:79:13 at f (C:\Program Files (x86)\nodejs\node_modules\npm\node_modules\once\once.js:16:25) at C:\Program Files (x86)\nodejs\node_modules\npm\node_modules\npmconf\npmconf.js:108:14 at Conf. (C:\Program Files (x86)\nodejs\node_modules\npm\node_modules\npmconf\npmconf.js:179:14) at Conf.next (C:\Program Files (x86)\nodejs\node_modules\npm\node_modules\npmconf\lib\load-prefix.js:48:20) at C:\Program Files (x86)\nodejs\node_modules\npm\node_modules\mkdirp\index.js:37:53 at Object.oncomplete (fs.js:107:15) C:\Program Files (x86)\nodejs\node_modules\npm\lib\npm.js:33 throw new Error('npm.load() required') ^ Error: npm.load() required at Object.npm.config.get (C:\Program Files (x86)\nodejs\node_modules\npm\lib\npm.js:33:11) at exit (C:\Program Files (x86)\nodejs\node_modules\npm\lib\utils\error-handler.js:49:27) at process.errorHandler (C:\Program Files (x86)\nodejs\node_modules\npm\lib\utils\error-handler.js:316:3) at process.emit (events.js:95:17) at process._fatalException (node.js:272:26) 7
So it looks like the PHP knows where NodeJS and NPM is... but can't run it?
If I do whoami in the PHP I get: nt authority\iusr
But if I do it from the command prompt then I get: dom\cameron
I wonder if it's because PHP runs it as a different user to when I run it directly in the command prompt...
Okay I've managed to fix this!
Basically you need to run the website (where the PHP file lives) as the same user as where NodeJS is run from!
To achieve this I did the following in IIS:
1.) Find the website
2.) Choose basic settings
3.) Click the connect as... button
4.) Choose 'specific user'
5.) Enter `dom/cameron` and my password
6.) Restart IIS
If anyone has any ideas for how to get NodeJS running for the default nt authority\iusr then feel free to post an answer :)
You defined npm in WINDOWS path, but did PHP recognize it?
I suggest you should test it with writing the full NPM path in the exec. If it works, than you should call putenv() with correct parameters before executing npm install..
So first try:
exec('C:\npm-test\npm install', $output);
$output = implode(PHP_EOL, $output);
echo $output;
And if it works, use the putenv() command before calling exec(). Example:
putenv("npm='C:\npm-test\npm'");
//and then exec(...)
The error you're getting indicating that an object is not defined. It's quite possible that a module is simply not loading because you're not running the script from the correct location, and relative links to some include are not working. Running as the correct user might fix it. Or try changing to the correct directory before running the node script. Or change the location of the links inside the node script.
I try to run MAGMI from a shellscript, but I get allways the same message:
/bin/sh: /is/htdocs/XXXXXXXXX/magento/magmi/cli/XXXXXXX.sh: /bin/bash^M: bad interpreter: No such file or directory
My script:
#!/bin/bash
FILES=/is/htdocs/XXXXXXXXX/magento/var/import/XXXXXXXXX.csv
for f in $FILES
do
echo "Running Magmi update with file: $f"
php magmi.cli.php -profile=XXXX -mode=update -CSV:filename="${f}"
wait
done
The script is in the same directory as magmi.cli.php
Thanks for hints and solutions !
Please use dos2unix linux command to remove above error like:
dos2unix scriptfilename.sh scriptfilename.sh
it generally occurs when we written something in WINDOW eniv. & then transfer to Linux platform
I'm currently trying to incorporate Gradle into a web based java IDE, so that a user could build their files.
I've tested the command to do it using Putty and it yielded the exact results I expected(creating a series of directories and sub directories).
$command = 'cd /home/neema/sites/javaIDE.local/branches/develop/javaIDE/app/java/User1/test1 2>&1';
$command2 = '/home/neema/sites/javaIDE.local/branches/develop/javaIDE/app/gradle/bin/gradle init --type java-library 2>&1';
However when i run the above commands in shell_exec in php(console.logging the response)i get this error.
FAILURE: Build failed with an exception.
What went wrong:
Could not create service of type InitScriptHandler using BuildScopeServices.createInitScriptHandler().
Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
I ran the stacktrace and this is the result, http://pastebin.com/YaHyWNR6.
I can't for the life of me understand why it won't work.
I've given the folder 'test1' all permissions (777) and now I've resorted to help :)
Thanks in advance to anyone with insight.
I'm in trouble and that much confused about a php shell_exec command.
When the command is execute by PHP I have no error but the execution fails. If I use exactly the same command from a terminal it works.
Here's the command :
/usr/bin/wkhtmltopdf --lowquality --dpi 300 --encoding utf-8 "/tmp/knplabs_snappyxa9otq.html" "/tmp/knplabs_snappyv3pD7h.pdf"
When I lauch this from a terminal :
$ /usr/bin/wkhtmltopdf --lowquality --dpi 300 --encoding utf-8 "/tmp/knplabs_snappyWG9XTd.html" "/tmp/knplabs_snappyv3pD7h.pdf"
Loading page (1/2)
Printing pages (2/2)
Done
But from my php script :
// Construct the previous command
$command = $this->buildCommand($url, $path);
../..
shell_exec($command);
../..
$content = file_get_contents($path);
../..
I've test the output of shell_exec, it's empty.
The log :
Warning: file_get_contents(/tmp/knplabs_snappyv3pD7h.pdf): failed to open stream: No such file or directory in /*****/lib/snappy/SnappyMedia.class.php on line 64
No permission pb in the /tmp directory :
$ ls -la /tmp
total 448
drwxrwxrwt 16 root root 4096 mars 12 21:51 .
../..
I've tried avec the PHP exec() function to get error informations, I just get an "1" error code in return_var and nothing in output.
For information this issue appear on my test server, my desktop computer but not on my notebook. All the 3 are with sames PHP, Apache, Mysql versions.
I don't understand anything ...
Thanks for any help, I'm loosing my mind.
David.
I've found the solution here : Executing wkhtmltopdf from PHP fails
Thanks to Krzychu.
First to get information from the shell_exec command add " 2>&1" at the end of the command. In that way you will get information in return of the command :
$no_output = shell_exec($command);
echo $no_output; // nothing
$output = shell_exec($command . ' 2>&1');
echo $output; // in my case : "cannot connect to X server"
The solution :
Not use the wkhtmltopdf ubuntu package (0.9.9-4)
Use the official package from the Wkhtmltopdf download page
So no need to install xvfb ! (I've seen this advice many times)
Looks like a user's permissions issue.
When you run the command from the terminal, it is the user account, currently used, which does have the right permissions, to run a command in /usr/bin, and execute the specific file.
When you run it from the php script, it is the http server account on your system, which needs the permission to execute the file in /usr/bin. Usually this is the apache user.
How you should setup permissions depends on your system. Just remember that what is allowed for apache, is allowed for anyone accessing your http server.
I have had this problem for ages and adding . ' 2>&1' after the $command has somehow solved the problem.
this:
$output = shell_exec($command . ' 2>&1');
instead of:
$output = shell_exec($command);
No idea why but it works and I'm grateful.
Is it a shared hosting? It seems like shell_exec is a restricted function. Try running error_reporting(E_ALL); ini_set('display_errors', 1); before calling shell_exec.
I stumbled upon the same Problem, in my case an absolut Path in the exec Command like /var/www did not work, I had to use relative Paths from the point where I executed the php File.
I also wanted to notice, that it did not work using shell_exec, however it worked using normal exec command, not sure wheres the difference here.
CentOS 5.9, PHP 5.4.21, Tomcat 7.0.42, Safe mode is off.
I need to commit some code to repository from php. but failed with exit code 128.
Codes are below, and command_exec is 'cd /data/project && git add .'
ob_start();
$this->system_call_detail = system($this->command_exec, $this->output);
$logger->debug('ExecuteCMD system call result : '.$this->system_call_detail);
ob_end_clean();
I can run git command as the apache user account from cmd, but programs run from PHP fail with exit code 128.
I guessed this cause from PHP. So, I tried this git command, "php -r 'system("cd /data/project && git add .", $test); echo $test;'" from cmd as apache user account and it success.
I solved this problem..
First, I couldn't get contents when run command, because of some php bug(?).
http://kr1.php.net/manual/en/function.system.php#108713
I fixed the command belows:
$this->system_call_detail = system($this->command_exec.' 2>&1', $this->output);
and then, I could get error msgs belows:
fatal: unable to access '/home/{username}/.config/git/config': Permission denied
But, there were no such files or directory.
So, I googled with the error msgs. and found some blog.
http://www.jethrocarr.com/2013/08/25/the-apache-that-wanted-to-be-root/
And I edited '/etc/sysconfig/httpd' file with the blogs posted.
And Solved. :)
Thank for helping me.
deceze and ojrask