I have been trying to run a python code that takes input from terminal like this
python3 mycode.py input1 input2 and it perfectly works when I try it on terminal. But it is not working when I try it on php's shell_exec.
My php-
<?php
$a = 'hello';
$b = 'world';
$output = shell_exec('python3 /path/to/mycode.py '.$a.' '.$b.'');
echo '<pre>'.$output.'</pre>';
?>
But it doesn’t output anything.
I have also tried adding
#!/usr/bin/python3
at the top of my python code and
sudo chmod +X mycode.py
but still not working.
Edit:
I found the problem but still don't know how to fix it. The mycode.py contains PIL module but it cannot be used on www-data user. So I tried sudo chown -R chris:www-data /var/www/html/mysite and now it is showing PIL module not found. How can I install PIL module for www-data?
The exec() function should return an array of the output of the command executed. With that I would recommend using print_r().
Please try the following:
<?php
$a = 'hello';
$b = 'world';
exec('python3 /path/to/mycode.py '.$a.' '.$b, $command_output);
echo PHP_EOL;
print_r($command_output);
echo PHP_EOL;
?>
In an attempt to rule out a permissions issue you can also execute the php script from your terminal. Assuming you are using Apache whereas the service account is commonly www-data the account may not have access to the file you are attempting to execute.
If that doesn't work for you attempt running the php script as sudo. If that fails make sure to review your php logs for any clues.
Related
I have a script that calls fswebcam to capture a jpg with my USB camera. I've made it executable with "chmod +x webcam.sh" :
File : /var/www/html/webcam.sh
#!/bin/bash
DATE=$(date + "%Y-%m-%d_%H%M")
fswebcam -r 640x480 /home/pi/webcam/$DATE.jpg
This is working fine in command line without sudo, so I've made a small PHP page :
File : /var/www/html/index.php
<?php
$output = shell_exec('sh /var/www/html/webcam.sh');
echo "<pre>$output</pre>";
?>
When I go to the webpage, I just get a blank page and no jpg is created in my webcam folder.
I got the following error :
Apache2 error log
So I've tried modifying my call in PHP, to :
<?php
$output = shell_exec('/usr/bin/sudo /bin/bash /var/www/html/webcam.sh');
echo "<pre>$output</pre>";
?>
I've also add the following to sudoers file
www-data ALL=NOPASSWD: /path/to/script
But I still get the error : apache2 log error
I've tried everything from this thread : How to run .sh script with php?
Do you have any idea ?
Thanks in advance,
Victor
First off:
Don't use sudo if you don't have a very good reason for it.
sh does not necessarily invoke bash.
sudo expects a password, but you didn't provide any hence the error.
I suggest trying with exec instead of shell_exec (there is a difference between the two):
<?php
exec('/var/www/html/webcam.sh', $output, $exitCode);
echo 'Exit code: '.$exitCode.' <hr />';
echo implode('<br />', $output);
Another source of your problem could be permission related:
The webserver usually runs as a different user.
Make sure the webserver can actually write to the output directory.
so I have read about 10 answers and everyone seems to suggest ideas which for some reason don't work.
i am trying to execute a simple command line which is "svn update" but it is not working and it returns NULL
so i have tried trial and error the way and for now this is what i can say;
i have tried several commands like
<?php
exec ("cmd /c ping 127.0.0.1 -n 1 > results.txt ");
?>
and
<?php
exec ("cmd /c chdir > results.txt ");
?>
and both work.. infact chdir says the exact position where the php file executing the line is stored on the pc..
so the problem now is, why do some commands like this:
<?php
exec ("cmd /c dir > results.txt ");
?>
don't work? this results and empty value even though inside the folder i have several files and directories.
and why if i use the command prompt to move into the folder where the php file is store and type svn update it works and doing
<?php
exec ("cmd /c svn update > results.txt ");
?>
return a NULL?
any help is really appreciated.
it feels like i have some restrictions dued to the configuration setup because when i try in local using apache i can get most of the commands to work (shell_exec, system, exec, even without the cmd /c)
Ok.
i have managed to solve the issue..
this is what i did:
first check exactly what username is running for the specific website.. to do so do:
<?php
$out = array();
exec('cmd /c whoami 2>&1',$out,$exitcode);
echo "<br />EXEC: ( exitcode : $exitcode )";
echo "<hr /><pre>";
print_r($out);
echo "</pre>";
?>
this will return the computername followed by the username..
now on the computer running the webserver run
control userpasswords2
and give administrator powers to the username whoami said
this will allow you to finally run any command you want using exec or system_exec
on the other hand continuing with my SVN command i found out that I had another problem which is that when you run it, it will look for the config file which is under the administrator account and will give an error saying:
svn: E125001: Can't determine the user's config path
to solve this issue you simply have to specify in the command the config_dir by doing this:
exec('cmd /c svn update --config-dir C:\Users\Administrator\AppData\Roaming\Subversion C:\\inetpub\\vhosts\\websitename\\httpdocs\\folder 2>&1',$out,$exitcode);
hope this helps others which are having problems like the ones i had!
This is likely a system user permissions issue. I tried your example:
<?php exec ("cmd /c dir > results.txt "); ?>
On my Windows7 with Xampp installed and it worked perfectly fine. However with IIS the "user" may not have permissions to the directory, off the top of my head I think it may be the system user IIS-IUSR or something like that.
Here is a link that might help with user permissions for IIS: http://www.iis.net/learn/get-started/planning-for-security/understanding-built-in-user-and-group-accounts-in-iis
Okay, based on your answers, I think you should try this:
Execute your command using the 'svn.exe' executable (replace the [[reposity location]]).
It is possible that your client has another svn.exe location, but you will have to figure that out yourself :)
exec('cmd /c "c:\\Program Files\\TortoiseSVN\\bin\\svn.exe" up "[[repository location]]"');
What happends now?
There is also a second parameter in exec, maybe you should also take a look at that one.
The only thing that helped me was, complete routes:
$template_file = "C:/archivos/archivos.tex"
$cmd = sprintf("C:/texlive/2020/bin/win32/pdflatex.exe " .$template_file );
$result = exec($cmd, $output, $a);
so I have read about 10 answers and everyone seems to suggest ideas which for some reason don't work.
i am trying to execute a simple command line which is "svn update" but it is not working and it returns NULL
so i have tried trial and error the way and for now this is what i can say;
i have tried several commands like
<?php
exec ("cmd /c ping 127.0.0.1 -n 1 > results.txt ");
?>
and
<?php
exec ("cmd /c chdir > results.txt ");
?>
and both work.. infact chdir says the exact position where the php file executing the line is stored on the pc..
so the problem now is, why do some commands like this:
<?php
exec ("cmd /c dir > results.txt ");
?>
don't work? this results and empty value even though inside the folder i have several files and directories.
and why if i use the command prompt to move into the folder where the php file is store and type svn update it works and doing
<?php
exec ("cmd /c svn update > results.txt ");
?>
return a NULL?
any help is really appreciated.
it feels like i have some restrictions dued to the configuration setup because when i try in local using apache i can get most of the commands to work (shell_exec, system, exec, even without the cmd /c)
Ok.
i have managed to solve the issue..
this is what i did:
first check exactly what username is running for the specific website.. to do so do:
<?php
$out = array();
exec('cmd /c whoami 2>&1',$out,$exitcode);
echo "<br />EXEC: ( exitcode : $exitcode )";
echo "<hr /><pre>";
print_r($out);
echo "</pre>";
?>
this will return the computername followed by the username..
now on the computer running the webserver run
control userpasswords2
and give administrator powers to the username whoami said
this will allow you to finally run any command you want using exec or system_exec
on the other hand continuing with my SVN command i found out that I had another problem which is that when you run it, it will look for the config file which is under the administrator account and will give an error saying:
svn: E125001: Can't determine the user's config path
to solve this issue you simply have to specify in the command the config_dir by doing this:
exec('cmd /c svn update --config-dir C:\Users\Administrator\AppData\Roaming\Subversion C:\\inetpub\\vhosts\\websitename\\httpdocs\\folder 2>&1',$out,$exitcode);
hope this helps others which are having problems like the ones i had!
This is likely a system user permissions issue. I tried your example:
<?php exec ("cmd /c dir > results.txt "); ?>
On my Windows7 with Xampp installed and it worked perfectly fine. However with IIS the "user" may not have permissions to the directory, off the top of my head I think it may be the system user IIS-IUSR or something like that.
Here is a link that might help with user permissions for IIS: http://www.iis.net/learn/get-started/planning-for-security/understanding-built-in-user-and-group-accounts-in-iis
Okay, based on your answers, I think you should try this:
Execute your command using the 'svn.exe' executable (replace the [[reposity location]]).
It is possible that your client has another svn.exe location, but you will have to figure that out yourself :)
exec('cmd /c "c:\\Program Files\\TortoiseSVN\\bin\\svn.exe" up "[[repository location]]"');
What happends now?
There is also a second parameter in exec, maybe you should also take a look at that one.
The only thing that helped me was, complete routes:
$template_file = "C:/archivos/archivos.tex"
$cmd = sprintf("C:/texlive/2020/bin/win32/pdflatex.exe " .$template_file );
$result = exec($cmd, $output, $a);
I'm new in using Linux, I'm trying to write a PHP code which can run .exe linux compatible file, I've made a short shell script
hello bash script:
#!/bin/bash
./program.exe file.mp4 // file.mp4 is an an input for .exe
echo "Hello World!"
shell.php:
<?php
$output = exec ("./hello ");
echo "<pre>$output</pre>";
?>
Now when I run shell.php using web browser it shows Hello World! but the .exe doesn't run, however when I run php using terminal command php shell.php, It works fine.
I think I'm having problems with permissions but I'm new with Linux and I don't know how to solve this.
Update:
I ignored the shell script and I used
<?php
$output = shell_exec ("cd /var/www/ && ./program.exe file.mp4 2>& " );
?>
also I granted access to program.exe
chmod 777 program.exe
the error I receive in the browser :could not open debug.bin!
use the absolute path to hello executable exec("sh path/to/the/file")
I'm using something similar to call an app compiled with mono on a remote ubuntu webserver and return it's output to the calling script.
For any of this to work properly wine needs to be already installed.
On Ubuntu systems try:
sudo apt-get -y install wine
You then need to know the owner of the web server process. If you are running the apache web server try the following:
cat /etc/apache2/envvars | grep "RUN"
The output will look something like this:
export APACHE_RUN_USER=www-data
export APACHE_RUN_GROUP=www-data
export APACHE_RUN_DIR=/var/run/apache2$SUFFIX
Now that you have the name of the process owner, which in this case is www-data you should ensure the file is owned the user and its group:
sudo chown www-data /var/www/program.exe
sudo chgrp www-data /var/www/program.exe
Finally, we can invoke the application from inside our PHP script by passsing it as a parameter to 'wine' and using its full file path.
<?php
$output = shell_exec("wine /var/www/program.exe file.mp4" );
?>
Any output from the above shell command sent to the command line will be saved in the PHP script variable $output.
It looks like you are trying to do some output redirection with your use of program.exe file.mp4 2>& so I've left that off of the example for clairity.
Try using the absolute path, such as exec("sh /path/to/file")
Generally, php is run as www or apache, so make sure that the execute access permission is granted to all user.
I am trying to Execute a multiple commands in php using exec() and shell_exec but i am getting a null value back which i shouldn't and nothing is happening (if i copy and paste the strings below in the command line it will work fine and accomplish the job needed) this is the commands i am using:
$command = "cd /../Desktop/FolderName;";
$command .= 'export PATH=$PATH:`pwd`;';
$command .= 'Here i execute a compiler;';
and then i use the escapeshellcmd()
$escaped_command = escapeshellcmd($command);
then
shell_exec($escaped_command);
any ideas what i am doing wrong and i also tried escapeshellarg() instead of escapeshellcmd()?
Solution: the Problem was the permission of the execution compiler for other owners is non and this was the problem.
because when you are using exec() function in php the owner of the file will be www-data so you need to give permission for the www-data either from the ACL of ubuntu or whatever linux based operating system(you can know the owner by doing this exec('whoami')), or by the files you need to execute.
(Sorry my bad English)
On Linux you can add your Commands in a Shell Script.
You can put this in any file:
#!/bin/bash
cd /../Desktop/FolderName
export PATH=$PATH:`pwd`
EXECUTE COMPILER
And save this as fille.sh
Then, add execution permissions:
chmod +x path/to/file.sh
From PHP, you can call this Script executing:
shell_exec('sh path/to/file.sh');
Hope this helps!