How to stop python script when run it from Website (php)

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

Upvotes: 2

Views: 2762

Answers (1)

Bart748
Bart748

Reputation: 11

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

Upvotes: 1

Related Questions