0

I'm running apache2 web server on raspberry pi3 model B. I'm setting up smart home running with Pi's and Uno's. I have a php scrypt that executes python program>index.php. It has rwxrwxrwx >I'll change that late becouse i don't fully need it. And i want to real-time display print from python script. exec('sudo python3 piUno.py') Let's say that output is "Hello w"

How can i import/get printed data from .py?

5
  • Is this any more complicated than employing command line redirection operators? Commented Jan 9, 2018 at 18:22
  • Please explain. Commented Jan 9, 2018 at 18:26
  • I don't know enough about what you are trying to end up with and why. Search on the topic (command line redirection operators) and perhaps try some additional things involving them and update your question with a specific question. Commented Jan 9, 2018 at 18:34
  • you are running a python script and want to see the output of another python script? Can't you just make the other python script a function and call it? Or just reload the module so it runs again? Commented Jan 9, 2018 at 19:03
  • Its a php file website that calls script to execute and return values to website-for control of what is is going on Commented Jan 9, 2018 at 19:18

2 Answers 2

2

shell_exec returns the output of your script. so use

$cmd = escapeshellcmd('sudo python3 piUno.py'); 
$output = shell_exec($cmd);
echo $output;

should work! let me know if it doesn't

edit: oh hey! your question got me looking at doc to check myself and exec actually returns the last line of output if you need only the last output.

$output = exec('sudo python3 piUno.py');
echo $output;  

or, you can set a second parameter to exec() to store all output lines in an array (1 entry per line) as this

$output = array();
exec('sudo python3 piUno.py',$output);
var_dump($output);

aight! this was fun!

Sign up to request clarification or add additional context in comments.

2 Comments

no i don't get any output. I tried adding escapeshellcmd('python piUno.py') but still not working
it returns the shell output. if there is none, there is no output. ie there is an error, or literally no output from the command you run. make sure you gave sudo rights to apache (if you realllllllly need to use sudo. if you don't just don't invoke sudo and it should run) to do this, use this walk-through bonebrews.com/granting-sudo-to-php. if your service is hosted to the open, you should not do that
0

First make sure you have permissions to write read execute for web user. You can you user sudo sudo chmod 777 /path/to/your/directory/file.xyz

For php file and file you want to run. $output = exec('sudo pytho3 piUno'); echo $output; Credits ---> Ralph Thomas Hopper

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.