3

So I have a a webpage that has buttons to run a script that controls a PIR sensor and an LED. How do I stop the script from running using a second button?

Button 1 : Run sensor.py Button 2 : Stop sensor.py script

I've looked up on it, and it seems that I need to find the PID, how would I do it via a web interface?

Regards

SensorON.py

while True(sensor value):
sensing code
turn led on()
print('motion detected')

elif os.path.exists('/var/www/gpio/SensorOff.py'):
    print 'stop-script detected'
    os.remove('/var/www/gpio/SensorOff.py')
    print 'stop-script deleted - now exiting'
    exit();

else
turn off led()
print('no motion detected')

Web page

if (isset($_POST['SensorON']))
{
exec('sudo python /var/www/SensorON.py');
}

if (isset($_POST['SensorOFF']))
{
exec('sudo touch /var/www/SensorOFF.py');
}
  • You wrote a script to do "button 1". So why not write another script, bind it to "button 2" and let that kill the first script? Either using pidof or let the first script create a pid-file. – Ghanima Oct 1 '14 at 12:57
  • @Wilf I haven't tried, I've only searched on various forums about it. Wasn't sure where to start. -Ghanima I wasn't sure how to find the PID and reference it in the button 2 script. I'll take a look on how to use that, thanks. – PiFanatic Oct 1 '14 at 14:30
1

IMHO the best way to do this is to have the running script periodically check for some flag that is set via the other script. One easy way to do that is to have the running script check for the existence of a file (stop-script, for example) which normally doesn't exist.

Then all you need to do in the script that is run when the Stop Button is pressed is to create (or touch) the file stop-script.

The running script should then delete the stop-script file just before it exits setting up for the next execution of the running script.

Here's what I envision in pseudo code (using python indentation for blocks):

sensor.py Script

while( TRUE )
  -- code to check sensor

  if exists '/path/to/stop-script'
     continue # exit while loop

  sleep(5)    # sleep for 5 seconds

print 'stop-script file detected'
delete file '/path/to/stop-script'
print 'stop-script file deleted - we will now exit'

button-2-pressed.py Script

execute_os_command("touch /path/to/stop-script")

Something like that.

Update: Answer to 2nd problem, namely web page hangs when running main script

Your second issue is that when in PHP you run exec('sudo python /var/www/SensorON.py'); , PHP will wait for the command to terminate, which in your case doesn't happen until later. You need to make this command run "asynchronously", see this for more detail, but effectively you need the following in your PHP web page:

if (isset($_POST['SensorON']))
{
    exec('sudo python /var/www/SensorON.py > /dev/null 2>/dev/null &');
}

if (isset($_POST['SensorOFF']))
{
    exec('sudo touch /var/www/SensorOFF.py');
}

By adding > /dev/null 2>/dev/null & to the command string redirects the output of the Python program to /dev/null (the bit bucket) and most importantly the final "&" character causes Python to run as a separate process and return is immediately returned to PHP allowing your web page to complete.

  • So while the button 1 script is running, it also checks for another python script? (Which is run when button 2 is pressed) Sorry I didn't get the second part, could you please clarify why the running script needs to delete the stop-script. – PiFanatic Oct 1 '14 at 14:40
  • I'll give it a go and let you know how it goes, cheers. – PiFanatic Oct 1 '14 at 14:56
  • Okay Im stuck, how would I create the stop-script file from the press of a button? I usually call it from a known location : if (isset($_POST['SensorOn'] exec('sudo python /var/www/SensorOn.py'); I've no idea how to actually create a script within one. – PiFanatic Oct 1 '14 at 16:15
  • When button 2 is pressed you want to run ' button-2-pressed.py' which should already exist and contain the commands to create the stop file. You could also just run exec('touch /path/to/stop-script') to shell out and create the file directly. The path to the file could also be /tmp which will ensure that the file is deleted upon reboot. – HeatfanJohn Oct 1 '14 at 16:22
  • Alright , works a treat! Appreciate the help. – PiFanatic Oct 1 '14 at 16:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.