Execute a php script from a bash script - php

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

Related

Why does this PHP script with a shell_exec call run from the command line in Windows 10, but not the browser/localhost?

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

executing a script via php exec which creates a file on the server

I have a php script which calls a shell script as below -
#!/bin/bash
timestamp=$(date +"%d-%m-%Y %H:%M:%S")
echo $timestamp >> results
The php script -
<?php
$mycmd = exec('/bin/bash exectest.sh',$op,$er);
var_dump($mycmd);
var_dump($op);
echo $er."\n";
?>
The php script returns error code 1 for $er but when i tried to modify the shell script to just print instead of writing to a file. the Php script then returns 0 and succeeds.
Any ideas of where I need to fix this?
I have tried giving the full path for the script and also this is the same case when i tried using a python script in place of a shell script.
Your observation indicates that this is most likely a permission problem, e.g. the user running PHP does not have write permission to either the results file in its current working directory or the directory itself (if the file does not exist yet).
It happened to be running on an AFS machine hence required afs priviledges for httpd.
fs sa . http write
sorted the issue.

How to run a php file from bash script?

I am working in a Windows machine. I need to run a script that will execute a php file. When I run where php to find my php executable location I get the following:
c:\xampp\php\phpe.exe
After that I tried something like this:
#!/bin/bash
#!/c:/xampp/php c:/xampp/htdocs/Bash/testingphp.php
I don't get any error, but I don't know if the script is doing something. Just for reference I am using cygwing to run the scripts. How do I know if my script is really working??
Thank you in advanced
As stated by #Etan, your second line is a comment, but your paths are also incorrect. You need to use /cygdrive/c/, not /c:/. Here is the corrected script:
#!/bin/bash
/cygdrive/c/xampp/php /cygdrive/c/xampp/htdocs/Bash/testingphp.php

ubuntu cant execute exec(test.sh) in php

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).

Can't run shell script from php web script

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.

Categories