I have a php script that creates a shell script file that finally executes as the www-data user, all of the commands are executed except for the last one which implies a binary file. If I run the command as root, it runs ok...
This is the last part of the script:
&& echo "Tokenizing the file........" >> Logs/table_of_contents.php \
&& perl ../common/Scripts/xmltokenize.pl --filename=xmlfiles/table_of_contents.xml >> Logs/table_of_contents.php \
&& perl ../common/Scripts/xmlrenumber.pl --filename=xmlfiles/table_of_contents.xml >> Logs/table_of_contents.php \
&& echo "Tagging the file........" >> Logs/table_of_contents.php \
# I have added this line to check if it helps but id doesn't
&& export HOME="/tmp/" \
# And this is the command that calls the binary file
&& perl tagfile.pl xmlfiles/table_of_contents.xml \
Here you have the content of the tagfile.pl
use File::Find;
$\ = "\n";
$fn = shift;
if ( $fn =~ /([^\/\.]+)\.xml/ ) { $fileid = $1; } else { exit;};
print $fileid;
$cmd = "perl tagfl2/makevrt.pl 'xmlfiles/$fileid.xml' > 'tagtmp/$fileid.vrt'";
print $cmd;
print `$cmd`;
#ALL OF THE PREVIOUS WORKS
#THIS IS THE ONE THAT GIVES PERMISSION ERRORS
# OF COURSE: "www-data:www-data tagtmp/" and "www-data:www-data $fileid.vrt = table_of_contents.vrt"
$cmd = "cut -f 1 tagtmp/'$fileid.vrt' | tagfl2/treetagger/bin/tree-tagger -no-unknown -token -lemma tagfl2/treetagger/lib/english.par > 'tagtmp/$fileid.tagged'";
print $cmd;
`$cmd`;
$cmd = "perl tagfl2/mrg.pl 'tagtmp/$fileid.vrt' 'tagtmp/$fileid.tagged' > 'tagtmp/$fileid.mrg'";
print $cmd;
`$cmd`;
$cmd = "perl tagfl2/tagxml.pl 'tagtmp/$fileid.mrg' 'xmlfiles/$fileid.xml'";
print $cmd;
`$cmd`;
Here is the error:
sh: 1: tagfl2/treetagger/bin/tree-tagger: Permission denied
Also, just in case:
chown -R www-data:www-data tagfl2/
chmod -R g+rwx tagfl2/
Try to define a full path to the script
$cmd = "perl /[full_path]/makevrt.pl 'xmlfiles/$fileid.xml' > 'tagtmp/$fileid.vrt'";
Why did you update user ownership?
Changing the group ownership should have been enough:
chgrp -R www-data tagfl2/
chmod -R g+rwX tagfl2/
And change the lowercase x by a greater one, to give access/execution permission, only if it is already the case for the user owner (no need to give otherwise).
You may then check the permission like this:
su -m -c 'ls -R tagfl2/' www-data
And see if you reproduce access issue; and then update permission accordingly.
Ok, all solved, one thing was giving the file system, actually the mounted unit, the exec attribution.
The second thing was moving treetagger directory to /usr/local/
Then, at /usr/local/bin/ I have created a soft link this way:
ln -s ../treetagger/bin/tree-tagger
Making the binary file globally executable. Actually, this last step was the ultimate solution.
Then at the tagfile.pl perl script, the line containing the tree-tagger command, I have changed it this way:
cut -f 1 'tagtmp/$fileid.vrt' | /usr/local/bin/tree-tagger -no-unknown -token -lemma tagfl2/treetagger/lib/english.par > 'tagtmp/$fileid.tagged'
Related
I am currently trying to use a "wine" command from my PHP-Script.
If i execute this:
$shell = shell_exec("/usr/bin/wine --version");
All is working fine and WINE version is displayed in $shell.
But, if i try to make it like this:
$run = shell_exec("/usr/bin/wine ".$workdir."/bin/tool.exe -m ".$workdir."/bin/std.maps -a ".$workdir."/bin/alias.file -n ".$workdir."/files/".$project_name."/upload/dump.bin -o ".$workdir."/files/".$project_name."/maps/definitions.list");
Which results in:
$run = shell_exec("/usr/bin/wine /var/www/html/bin/tool.exe -m /var/www/html/bin/std.maps -a /var/www/html/bin/alias.file -n /var/www/html/files/1-59374-94700/upload/dump.bin -o /var/www/html/files/1-59374-94700/maps/definitions.list 2>&1");
I get the following output:
wine: chdir to /.wine : No such file or directory
What i am doing wrong? If i enter the command above directly to the shell, all is working fine. If i do it without /usr/bin/ in front of wine, the output's are the same.
Br, Chris
I haven't try it myself but I think it is because wine is run as different user with different environment settings. Try create .wine directory inside /var/www and make www-data user as owner of this directory and make /var/www/.wine HOME directory.
$run = shell_exec("HOME=/var/www/.wine /usr/bin/wine ".$workdir."/bin/tool.exe -m ".$workdir."/bin/std.maps -a ".$workdir."/bin/alias.file -n ".$workdir."/files/".$project_name."/upload/dump.bin -o ".$workdir."/files/".$project_name."/maps/definitions.list");
Here is my code:
$pdf = '/Users/macbookpro/Desktop/q.pdf';
$swf = '/Users/macbookpro/Desktop/q.swf';
$command2 = 'pdf2swf -o '.$swf.' -T -z -t -f '.$pdf.' -s flashversion=9';
exec($command2,$out,$status);
var_dump($output);
The output is NULL and no SWF is generated. However, if I output the command and copy it to terminal, it works. How do I solve this?
exec runs as the user running the script. Apache user likely doesn't have the PATH variable telling it where to look for programs, so instead of
$command2 = 'pdf2swf -o '.$swf.' -T -z -t -f '.$pdf.' -s flashversion=9';
Try adding the location of pdf2swf, something like:
$command2 = '/bin/pdf2swf -o '.$swf.' -T -z -t -f '.$pdf.' -s flashversion=9';
And make sure that the apache user has permission to get to the executable, and permission to execute it.
chmod a+x /bin/pdf2swf
Of course replace /bin/ with where ever pdf2swf really lives for all the example code in this answer.
i try to execute a grep command inside a php shell_exec. And it works fine besides it fails when i have a underscore in the search word. I can not seem to figure out why this fails because of a underscore since, a grep command with a underscore in search word works in shell code below:
$output = shell_exec("grep -l -r '$search_word'");
The content in search_word variable is dynamic from database but the word that gives me trouble is base_64
Try like this:
$output = shell_exec("grep -l -r '$search_word' ./*");
Before PHP spawns a subprocess your command will be $search_word evaluated:
grep -l -r '....'
# So in $search_word is set to `john doe` it will become:
grep -l -r 'john doe'
How PHP behaves I'm not sure, it might be stalling waiting for the process to finish, it might have been closing stdin already.
Your above command will expect input from stdin because no file name is specified, breakdown:
grep [option]... [pattern] [file]...
-l will only print file name of the matched file
-r recursive search.
TLDR: You properly want to specify a file / directory to search in:
$output = shell_exec("grep -l -r '$search_word' .");
// Or maybe
$output = shell_exec("grep -l -r '${search}_word' ."); # will use $search variable as an input from PHP while _word is a string now.
I have used php's exec to execute FFmpeg command but its not woking when I open it in browser. But when i run this php file script in terminal it works fine.And my php safe mode is off. please help me to get it solved. my php code is
<?php
$output=exec("ffmpeg -f image2 -i /home/phedra/imgs/image/img%03d.png -r 12 -s 610x489 /home/phedra/imgs/video/out.avi", $out);
echo $out;
echo $output;
?>
try giving full path where the ffmpeg application is located.
e.g.
/usr/bin/ffmpeg
So your function might look like:
$output=exec("/usr/bin/ffmpeg -f image2 -i /home/phedra/imgs/image/img%03d.png -r 12 -s 610x489 /home/phedra/imgs/video/out.avi", $out);
You must check what is the location of "ffmpeg".
I had this problem and it turned out it was Apache's permission on the public directory.
Note: I am running Ubuntu 14 on AWS
After installing FFmpeg I had to change the /var/www/* ownership to www-data.
sudo chown -R www-data:root /var/www
(the www-data is the important part here)
Then I had the following code running, and it works when I access it via URL (Apache)
// test.php
$run = system("/opt/ffmpeg/bin/ffmpeg -i /var/www/html/input.mp4 -vf scale=640:480 /var/www/html/output.mp4 &");
if($run) {
echo "success";
} else {
echo "failed";
}
The /opt/ffmpeg/bin/ffmpeg is where my FFmpeg is running from. Yours might be /usr/bin/ffmpeg or something else. You can locate it by typing locate ffmpeg in the command line and looking through the list it gives you.
The input file was a public .mp4 file and the output.mp4 file was going to the same location.
Run this in your command line: php test.php - works
Run this from your browser: yourwebsite.com/test.php - works
Note that if you are on windows you must use COMMAS. I.E:
$output=exec('"/usr/bin/ffmpeg" -f image2 -i /home/phedra/imgs/image/img%03d.png -r 12 -s 610x489 /home/phedra/imgs/video/out.avi', $out);
Like #Arfeen mentioned in his answer, you should execute the command with the path of ffmpeg, but, the given path in the answer "/usr/bin/ffmpeg" is not always the same.
First locate your ffmpeg by using the command :
which ffmpeg
The result in my case is :
/usr/local/bin/ffmpeg
Then go back to your php code and replace "ffmpeg" in the command by the path of ffmpeg (which is /usr/local/bin/ffmpeg in my case).
I want to launch the command "unoconv" from a script php.
$command = '/usr/bin/unoconv --server localhost --port 2002 --format=pdf file.rtf >/dev/null 2>/dev/null';
$rc = system( $command );
echo $rc;
The command return no result and the file is not created.
I think is a problem from access with www-data and unoconv.
When I'm launching the command in shell, the file is created.
Any idea?
You can add command unoconv to sudoers.
I do this in this way:
I create wrapper bash script in for example /usr/local/bin where I have command unoconv.
#!/bin/bash
if [ -z "$1" ]; then
echo "Must pass file";
exit 10;
fi
/usr/bin/unoconv -f pdf $1.rtf
after this I adding entry in /etc/sudoers.d:
www-data ALL=NOPASSWD: /usr/local/bin/unoconv.sh
And now you can call script in php:
exec('sudo /usr/local/bin/unoconv.sh '.$fileName);
Try to run
$output = `/usr/bin/unoconv --server localhost --port 2002 --format=pdf file.rtf`;
instead and see error messages.
For me works like this:
$cmd = "/usr/bin/unoconv -f docx files/thefile";
shell_exec($cmd);
of course you have to do this previously (if you lounch your php script from the web):
chown -R www-data:www-data files/
I have found a solution to this problem when running Apache. You have to create the home folder for the www-data user
sudo mkdir /home/www-data
sudo chown www-data /home/www-data
Lastly we will have to edit the home directory and default shell for the www-data user
sudo vim /etc/passwd
For the entry of www-data the last two strings have to be replaced respectively with
/home/www-data
/bin/bash
Simple as this
$output = shell_exec('/opt/libreoffice5.0/program/python unoconv -f rtf test.html');
Edit the path to suite your configuration.
It just works!
You may be running into an issue with LibreOffice, OpenOffice or soffice not being able to write to the current user's $HOME directory.
By running the command below I was able to identify the correct $HOME directory and see the error that was being generated.
$cmd = 'echo $HOME & unoconv -vvvv --format %s --output %s %s 2>/tmp/unoconv.debug.txt';
exec($cmd);
The verbose output of $cmd will be generated written to the file: /tmp/unoconv.debug.txt.
In my case the output was:
Verbosity set to level 5
DEBUG: Connection type: socket,host=127.0.0.1,port=2002,tcpNoDelay=1;urp;StarOffice.ComponentContext
DEBUG: Existing listener not found.
DEBUG: Launching our own listener using /usr/lib64/libreoffice/program/soffice.bin.
Failed to connect to /usr/lib64/libreoffice/program/soffice.bin (pid=32012) in 6 seconds.
Connector : couldn't connect to socket (Success)
Error: Unable to connect or start own listener. Aborting.
The command ran seemed to fine as root, and as sudo -u nobody. On seeing this output I realized there was an issue with the home directory.
Kudos to Dag Wieers for his help - I'm hoping this helps other unoconv devs with their debugging.