This is my php script
<HTML>
<head>
<h1>Hello</h1>
</head>
<body>
<?php
$command=escapeshellcmd('~/PycharmProjects/Test_1/wiki_update.py 2 2>&1');
$output=shell_exec($command);
echo $output;
?>
</body>
</HTML>
I get no output when I run it through the browser.
When I change the
$command=escapeshellcmd('whoami');
it outputs as NOBODY,
I granted permission to nobody for that python script still no output.
I am using xampp for linux and php script in htdocs/xampp.
Are you sure you don't need to change your command to run python?
$command=escapeshellcmd('python ~/PycharmProjects/Test_1/wiki_update.py 2 2>&1');
You also might try specifying absolute path - the ~ alias changes based on current user (which is the web server's user, not yours).
Related
I've made a simple webpage that has to run some simple bash scripts. I run apache2 on Jetson Nano and I have three files in /var/www/html folder:
index.html
testexec.php
test.sh
First one looks like:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>TEST</title>
</head>
<body>
<form action="testexec.php">
<input type="submit" value="Create file">
</form>
</body>
</html>
the php file:
<?php
$output = shell_exec("test.sh");
header('Location: http://192.168.25.16/index.html?success=true');
?>
script:
#!/bin/bash
touch file.txt
My problem is that everything looks good, but the scrit doesn't run. In future it will be used to run program written in python, but for now I can't run even that simple one. Is the problem with location of files or with something else?
I've already tried to change php file
$output = shell_exec("test.sh");
with
$output = exec("test.sh");
with or without $output
My browser (firefox) returns no errors in console.
Script works fine when I run it from shell. It is executable.
I've already tried to look for similar problems, but there were no solutions.
Very often the problem is with user rights, first try installing chmod 777 test.sh to just check if this is the problem.
In addition, you should go back to the exec documentation
exec(string $command, array &$output = null, int &$result_code = null): string|false
and check what $result_code is equal to.
If we get echo result!:
$output=null;
$retval=null;
exec('echo $(pwd);', $output, $retval);
echo "${retval} with value:\n";
print_r($output);
I want to call the python file from php using exec() but the output always is blank, and i do not know what is the problem, can anybody help me?
This is php code
<?php
exec("D:\python27\python.exe C:\xampp\htdocs\hi.py");
?>
This is python code
print "Content-Type: text/HTML"
print
print"""
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
"""
I can run the python file along and the browser will display hello world!, however when i using exec() in php, it would not work.
as you said you are running it like apache->php->shell_exec(SUDO..)
So the apache user has to be in sudoers file, better you don't give sudo to apache instead give apache (www-data) user right to run your python program
put first line in your python script: #!/usr/bin/env python so the script knows which program to open it with..
then
change group:
chgrp www-data /path/to/python-script.py
make it executabel
chmod +x /path/to/python-script.py
try it
shell_exec("/path/to/python-script.py");
I hope it works ;)
TIPP: Apache and PHP are for delivering Documents and Strings, if you want some kind of control and an API start with nodejs and https://www.npmjs.com/package/rpi-gpio package. This way you will have one place for your solid automation environment
My web host supports PHP. I have successfully run info.php and get pages of stuff to prove it in my browser.
When I try to execute a tiny piece of PHP in a tiny piece of HTML, as in the following hallowed script, I see nothing at all in my browser. Why? Is there something I am supposed to do?
Note: I use RapidWeaver as my principal site generation tool.
<html>
<head>
<title> PHP Test Script </title>
</head>
<body>
<?php
print "Hello World!";
?>
</body>
</html>
Make sure that your file extension is indeed .php, so that the web-server knows how to handle the file. Also, check your error-logs, there might be something in there if you get no output at all. You can do that in your terminal by running php --info | grep error. On your file-system, the error-log is stored in either /var/log/httpd/error_log or /var/log/apache2/error_log (if you're on Linux).
I'm running lighttpd 1.4.35 on Debian 8.2. I have a very simple html file with php code (php5) which calls a bash script and prints the output:
<html>
<body>
<?php
$res = shell_exec("/var/www/html/run.sh");
echo $res . " is the result";
?>
</body>
</html>
If that html file is called on firefox, the output is
is the result
If I directly run php with that file (php index.php), the output is
<html>
<body>
13.00
is the result</body>
</html>
So, where does the result get lost?
edit:
The webpage source code from firefox is
<html>
<body>
is the result</body>
</html>
edit: solved. bash script uses '~' which expands to wrong directory when the script is run from webserver.
The exec functions "only" return the contents of stdout, which is why you might miss an error message.
But you can redirect stderr to stdout, e.g. via
$res = shell_exec("/var/www/html/run.sh 2>&1");
shell_exec does not run if you're in safe mode, that could be the issue
I want to run a python script in a button press event of a php/html file. I tried following. Is this possible with PHP exec()? But following didn't work. What is wrong with my code?
HTML file
<html>
<head></head>
<body>
<H3>Run Python Script...</H3>
<form name="input" action="test2.php" method="get">
<input type="submit" value="Submit">
</form>
</body>
</html>
PHP exec()
<html>
<head></head>
<body>
<H3>Executing...</H3>
<?php exec("first.py"); ?>
<H3>Completed...</H3>
</body>
</html>
exec Executes a command as if on the command line, running first.py just via bash won't work. You have two options:
Option A
You can tell the python parser explicitly to run the file with:
exec('python first.py');
Option B
Make sure your python script's first line is the path to python:
#!/usr/bin/python
#Python code...
Then call exec('./first.py')
(If you chose this option, you'll need to make sure the script is executable)
Conclusion
I'd probably go with option A as it's simplest and gets the job done without fuss.
Hope this helps :) x
There can be several problems with even your python script.
First: Can you run this script from console? (Do you have a #!/path/to/env/python in the script's beginning)?
If not, then either add it to your script or to the exec function (without #!)
You can also use passthru instead of exec, so you can display the raw output of the script.
try exec("/usr/bin/python path/to/first.py")