In my php code:
exec(test.sh);
and test.sh has code:
echo "Hi this is test?" | espeak --stdout > demo.wav
But nothing happen. No error, No output.
If i try to execute test.sh from terminal that it will work perfectly. So why it not run on my php.
Can someone help me?
How do you run your PHP script? With php-cli (in shell mode)? HTTP (Apache, ...)?
It could be a path problem. Could you give us the path of your test.sh, the path of your php script or the URI which is called to run the PHP if you use apache or other HTTP server?
the apache user will need write access to the current working directory (presumably the same directory that contains your php script and test.sh).
Related
I'm using XAMPP on Windows 10, and trying to run a simple PHP script that uses the shell_exec function to call a simple R script in the same directory. Here's the PHP index.php file:
<?php
echo shell_exec('Rscript test.R');
And here's the test.R file:
print("Hello, World!")
From the command line (i.e., Git for Windows), when I run php index.php in the folder, I get the following output:
[1] "Hello, World!"
However, when I run index.php from the web browser via XAMPP and localhost, I don't see anything on the page. At first, I thought it might be a permissions issue, so I gave Full control access to all users on the folder, but it didn't seem to change anything.
Any ideas on how to get this working from the browser? Thank you.
Found the answer to the problem I was having. Apparently, even though Rscript is part of my Windows 10 PATH variable, when I try to execute an R script via shell_exec in PHP, I have to have the whole path to the Rscript executable (I guess because the PHP script / Apache server doesn't know about the path variable).
I changed the shell_exec function call as follows, and it worked:
echo shell_exec('"C:\Program Files\R\R-4.1.0\bin\Rscript" test.R');
Here's a link to the SO post that helped me figure this out:
shell_exec does not execute from browser requests
I am unable to execute a source command in linux using php.All other commands are working except this one. I need to execute the following command.
source /root/Envs/ate/bin/activate
This activates the ate-Automatic Test Equipment.Once I activate it then I need to run a python script as the script accesses the remote server.
I am able to manually run it but I am creating a tool which will automatically do it.
<?php
exec("source /root/Envs/ate/bin/activate", $output, $return);
echo "Command returned $return, and output:\n";
echo exec("python box_upgrade-pradeepa.py");
?>
The above commands returns 1 which means there is an error.But I am not sure how to run the 'source command'. The python script will run only if the source command is successful.(the python command is correct as I replaced hello.py and it ran fine.)
Could you pls help me as I am really stuck for a week?
Thanks a lot..
I found out the error. Since I am doing it using php (for a web tool) the user is Apache. 'Apache' user is unable to access the script in root folder. Moving it to another directory, I am able to run the script fine.
Thanks all..
First I tried other questions that may lead to an answer but failed. Anyway I have a problem executing a php script from a bash script and outputs the following error message: Could not open input file: /root/dir/file.php, I can execute the php script fine from terminal however I tried using the 2 following codes in the shell script to execute the php script:
wordpress="/etc/alternatives/php /root/dir"
$wordpress/file.php $input
and
php /root/dir/file.php
and they both failed, any help appreciated.
I'm guessing that your /root/dir means doc root. The kernel knows nothing about apache root dirs so does not know where to go. In the bash script you
a) do not need to use a path to php, it is already registered
b) you do need to use the absolute path to the script, unless it is in a directory that is listed in the PATH variable ( you can use a symlink asd well of course)
So try
php /absolute/path/to/script.php
This is really simple but I cannot get it to work at all. Spent many hours and I've always give up. I created php script called copy.php and it should call a python script called copy.py.
I want to execute a command line like this
<?php exec('/var/www/html/copy.py'); ?>
Really simple.
Why cannot I get the python script executed from php exec()? The function inside python script is to get a copy of error_log from a different directory (outside of Apache) into html directory.
If I run that from a terminal
> php copy.php
It did execute the function and made a copy. Why is that the web browser isn't doing it?
Let me simplify this:
why cannot exec("cp /var/log/httpd/error_log /var/www/html/path/to/php/script") work?
it works fine if I type it in terminal but not when run in a browser.
As others have alluded to, the difference is probably permissions. When you run a command from the command line, you're generally not the same users as your apache script is running as.
Put another way, if from the command line you type whoami, you'll probably get whatever name your user account is.
The echo exec('whoami'); from within php shows who the script is running as, which is Apache.
So, whatever command you're trying to run from your web server isn't available to run as the Apache user. You mentioned you've been able to have exec("python /usr/diskpurge/script.py") work, but not to have exec('/var/www/html/copy.py') doesn't. This is due to in one instance you're running python, in the other you're trying to execute your copy.py script. If copy.py doesn't have execute permissions for the Apache user, you're not going to be able to run it from the browser.
Perhaps different settings apply for the Apache environment versus the command line.
Use error_reporting(E_ALL); and ini_set('display_errors', true) to see what errosr may come up.
It is possible that the Apache environment is prohibited from using exec or the fact that Apache runs under a different user that does not have execute rights on the python script.
sounds like a permission error. Check if your server is running with sufficient rights.
echo exec('whoami');
Set your error reporting to report all:
ini_set('display_errors', true);
error_reporting(E_ALL);
and check for errors..
If your whoami returns a user which is not a member of the SU family (linux) or administration (windows) then resite your permissions..
Linux:
Assign the user returned by whoami correct permissions to run python scripts.. Do not allow the resulted username to run as root with total administration powers.. This is a big no no
The only reason its not working is because you didn't set the write permissions!
Do:
sudo nano /etc/sudoers
And then put the following:
www-data ALL=(root) NOPASSWD:ALL
I am trying to run a shell script from a php script.
I have complete control of the environment (unix on mac), I should have all the permissions, etc. set correctly.
The web script is in /htdocs/
The shell script can be executed from anywhere so when I go to /htdocs/ in the shell, I can easily run it like this:
$ my_shellscript
.. but when my php script (which is located in htdocs) tries to call it:
shell_exec('my_shellscript');
I get nothing.
I have proven the script can be called from that location and I have temporarily granted full access to try to get it working somehow. I am going crazy, please help.
If you know of some other way of triggering a shell script via the web that would be fine.
Thanks in advance.
well i got few weeks same problem, the solution is to check if the apace has the permission to execute your script. You could also try to run the script in php cli.
Since it is a shellscript, it needs to be invoked with the path prefix. My guess is you need to do this:
shell_exec('./my_shellscript');
First thing: make sure php isn't running in Safe Mode
Next thing: Try running it with the exec() function and using the full path (e.g. /var/www/htdocs/my_shellscript)
Try doing
echo shell_exec('my_shellscript 2>&1');
which will capture the script's stderr output and print it out. If something inside the script is failing, this output would otherwise be lost when not being run interactively.