I have the following PHP script:
#!/usr/bin/php
<?php
echo shell_exec(
"/usr/bin/git clone --bare ".
"/home/dave/create_project/template_project ".
"/home/dave/create_project/my_test_project.git"
);
About 7 in 10 times that I run it, git gives the following error:
find: write error: Broken pipe
This error never occurs if I run the equivalent command directly from the shell.
I have already tried:
using other PHP execution functions: exec, system, popen;
passing the whole command as an argument to bash, i.e., exec('bash -c '.$cmd);
Does anyone have any idea what might be going on?
It may depend on your exact platform, but findutils has been known to throw that kind of error message before.
On Fedora, that rpm package version 4.2.33-2.fc9 fixed the issue.
Does PHP throw any errors? Maybe max_execution_time is too low? Mu guess PHP app exites prematurely.
Related
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
I tried to generate LLVM intermediate code by calling
llvm-gcc -emit-llvm -I/mypath/ -c main.c -o main.o
It works perfectly without any warnings or errors if I manually type this command in the terminal.
However I have built a website that can automate this process by calling exec function in PHP like below.
exec("llvm-gcc -emit-llvm -I/mypath/ -c main.c -o main.o",$msg,$ret);
It will still generate .o file with a warning. The warning says that
Potential incompatible plugin version. GCC: 4.5.3. Expected: 4.5.4
Defines 'dragonegg_disable_version_check' as env variable to remove this warning.
Please note that unexpected errors might occur.
The php command will still return 0, which is successful. However when I run the generated .o file, it will throw invalid bitcode signature error.
The server is running ubuntu and Apache. My llvm-gcc version is 2.9 which uses 4.2.1 gcc.
Any help will do. Thank you!
The best thing would be to update the GCC to 4.5.4. But if you just want to hide the warning, simply define dragonegg_disable_version_check.
$command = 'dragonegg_disable_version_check=1'
. ' llvm-gcc -emit-llvm -I/mypath/'
. ' -c main.c -o main.o';
exec($command,$msg,$ret);
For the invalid bit code signature error -- see this questions.
i read a lot about that, but my problem still remain:
what i'm trying to do is:
echo shell_exec("/usr/bin/nohup /usr/bin/java -Xmx1g -jar /var/www/html/myDir/ff.jar &");
it gives me that error:
Error occurred during initialization of VM Could not reserve enough space for code cache
i use nohup because jar must run even if php terminate.
What could be the problem?
Do you have SELinux enabled on the machine it's running on? See http://archimedeseureka.blogspot.com/2011/01/executing-java-from-php-in-rhelcentos.html - I had this trouble on a Fedora 18 system and setting httpd_execmem seems to have fixed it.
I'm trying to run a Python script using exec() from within PHP. My command works fine when I run it directly using a cmd window, but it produces an error when I run it from exec() in PHP.
My Python script uses NTLK to find proper nouns. Example command:
"C:\Python25\python.exe" "C:\wamp\projects\python\trunk\tests\find_proper_nouns.py" "I went to London this morning"
returns [London] when I run it from cmd, but throws an error in the Apache log when I run the same command from exec().The script is defintely getting run OK - if I change the python script to be print "Hello World" that is returned fine.
I know it's a big ask for anyone to know how to fix this NLTK error, but I could really do with any pointers as to why running it from exec is different to cmd. (The command is identical).
I'm running WAMP on Windows 7 with Apache 2.2.11.
Here's the error in the Apache log:
Traceback (most recent call last):
File "C:\wamp\projects\python\trunk\tests\find_proper_nouns_command_line.py", line 6, in <module>
parts = nltk.pos_tag(text)
File "C:\Python25\lib\site-packages\nltk\tag\__init__.py", line 62, in pos_tag
tagger = nltk.data.load(_POS_TAGGER)
File "C:\Python25\lib\site-packages\nltk\data.py", line 590, in load
resource_val = pickle.load(_open(resource_url))
File "C:\Python25\lib\site-packages\nltk\data.py", line 669, in _open
return find(path).open()
File "C:\Python25\lib\site-packages\nltk\data.py", line 451, in find
raise LookupError(resource_not_found)
LookupError:
**********************************************************************
Resource 'taggers/maxent_treebank_pos_tagger/english.pickle' not
found. Please use the NLTK Downloader to obtain the resource:
>>> nltk.download().
Searched in:
- 'C:\\nltk_data'
- 'D:\\nltk_data'
- 'E:\\nltk_data'
- 'C:\\Python25\\nltk_data'
- 'C:\\Python25\\lib\\nltk_data'
- 'C:\\Windows\\system32\\config\\systemprofile\\AppData\\Roaming\\nltk_data'
**********************************************************************
You have to run nltk.download() and choose 'maxent_treebank_pos_tagger'. You must make a python script and in it put:
#!/usr/bin/python
import nltk
nltk.download('maxent_treebank_pos_tagger');
then run it from command line. It will install the data files for the POS tagges, which you don't have installed yet.
After you do this it should work.
Your web server likely runs with other privileges than yourself. Possible problems include:
Path/file permission: can the web server user access the files it needs?
Different environment: are all necessary environment variables (PATH, Python-specific stuff, …) set?
Configuration: are there per-user configurations for Python or the module?
Tip: execute set in both the command prompt and from the PHP process and check the differences.
From the shell/terminal, you can use:
sudo python -m nltk.downloader maxent_treebank_pos_tagger
It will install maxent_treebank_pos_tagger (i.e. the standard treebank POS tagger in NLTK).