justanotherstudent
Posts: 4
Joined: Thu Apr 13, 2017 1:56 pm

How can i be notified after successfully executing python file from a button on a webpage?

Thu Aug 24, 2017 8:44 am

Hello,

i have a button on a webpage that when clicked, it will run a python file.

#php code to handle it
<?php
if (isset($_POST['LightOn'])){
exec("sudo python /home/pi/lightOn.py");
}
?>

how can i be notified after successfully running the lightOn.py file?
Informatik 2012

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: How can i be notified after successfully executing python file from a button on a webpage?

Thu Aug 24, 2017 8:50 am

Have a look a the documentation for the exec function.

It returns every line of the command's output. You could have your python script output "success" or "failed" and use that value to create a notification.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

justanotherstudent
Posts: 4
Joined: Thu Apr 13, 2017 1:56 pm

Re: How can i be notified after successfully executing python file from a button on a webpage?

Thu Aug 24, 2017 9:59 am

scotty101 wrote:
Thu Aug 24, 2017 8:50 am
Have a look a the documentation for the exec function.
yes i have checked, so bare with me okay...this is what i come up with

<?php
if (isset($_POST['LightOn'])){
exec("sudo python /home/pi/lightOn.py", $output, $return);
}elseif (!$return) {
echo "Success";
}else{
echo "Failed";
}
?>

the $return will return any number than 0 if there is any error right?
Informatik 2012

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: How can i be notified after successfully executing python file from a button on a webpage?

Thu Aug 24, 2017 12:26 pm

You'd need to try it but I believe so.

You can test it by exiting from a python program with a "non-zero" exit code.

Code: Select all

import sys
sys.exit(1)
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

Return to “General programming discussion”