2

My Project is Lighting Control on Rasp Pi by use Rasp Pi as Web Server & Control the light by python script I use the UI on PHP Website to control the light

Here is my php code

<?php
    public function auto1()
    {   
        system('sudo -u root -S python /var/www/4led/chktime1.py');
    }

    public function disauto1()
    {   
        system('echo raspberry | sudo -u root -S pkill -f chktime1.py');
    }
?>

when I press the on button on website to turn auto script It call auto1 and work correctly but this is loop script (I intend to make it loop alltime) but when I press the off button It can't close it because the loop of chktime1.py is still working and cannot open other script or command in disauto1. How can I stop this script from PHP command.

Thank you for your helping

6
  • 1
    Even when a request is aborted, you have no guarantee that the python script will be halted. You have no control externally. Commented May 24, 2016 at 18:48
  • Why was this question tagged with codeigniter? Edited. Commented May 24, 2016 at 19:00
  • @LuisMasuelli Sorry for my poor code I can't make the smarter code than this script .This script will automatically check time from Database every 10 min . If the current time is concurrent with database time the light will open.I try from putty It can interrupt by "ctrl + c" or use command "echo raspberry | sudo -u root -S pkill -f chktime1.py" from other terminal It work very well. But I want to know how to exit the script in same terminal with still looping could you know the other way to stop this script. Commented May 24, 2016 at 19:02
  • @Sparky because I use Codeigniter Framework on this website Commented May 24, 2016 at 19:07
  • Your code is not poor, but it is irrevant to the answer. Even the smartest php code cannot guarantee what you want Commented May 24, 2016 at 19:12

1 Answer 1

1

Not sure if your problem is solved but maybe it helps someone else. You can Touch a File in PHP and check in your chktime1.py if the file is there or not.

<form action="" method="post">
     <input type="submit" name="start" value="start" />
     <input type="submit" name="stop" value="stop" />
</form>

<?php
    if(isset($_POST['start'])){
        system('sudo -u root -S python /var/www/4led/chktime1.py');
    }
    if(isset($_POST['stop'])){
        system('sudo -u root -S touch /var/www/4led/stop-script');
    }
?>

you can add this to your Python

import os, sys

while(true): 
   #your code
   if(os.path.isfile('/var/www/4led/stop-script')):
      break

os.system("sudo -u root -S rm /var/www/4led/stop-script")
Sign up to request clarification or add additional context in comments.

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.