symeboy
Posts: 8
Joined: Mon Mar 09, 2015 9:28 pm

Crash out of loop with sudo halt = bad?

Tue Apr 11, 2017 8:56 pm

Hi all,

I need to exit a loop and shut down on a button press, the python script below works, in that it forces a sudo shutdown directly. Is this OK or do I need to find a way to exit the loop first? Ive tried sys.exit() and various attempts using kill -SIGSTOP but neither work. Might not be a problem but because its a headless outdoor project I'd like to reduce the risk of failure.

Thanks in advance

import time
import smtplib
import sys
import os
import datetime
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)

os.system("cd /home/pi/detector")
GPIO.setup(17, GPIO.IN)

def stop(self):
print "Program stopping"
GPIO.cleanup()
os.system("sudo halt")

GPIO.add_event_detect(17, GPIO.FALLING, callback=stop, bouncetime=400)

try:
while True:
s = ''.join([ch if ch != ' ' else '_' for ch in str(datetime.datetime.now())])
os.system("AUDIODRIVER=alsa AUDIODEV=hw:0,0 rec /home/pi/detector/Wavs/" + s +".wav -G sinc 13k silence 1 0.1 20% trim 0 5")
time.sleep(4)

except KeyboardInterrupt:
print "You've stopped the program by CTRL-C"

GPIO.cleanup()
print "GPIO cleanup. All done."

User avatar
DougieLawson
Posts: 39121
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Crash out of loop with sudo halt = bad?

Tue Apr 11, 2017 9:08 pm

If your program doesn't write any data then there's no problem tearing its legs off while it's running.

I'd use "sudo poweroff" rather than "sudo halt".
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

symeboy
Posts: 8
Joined: Mon Mar 09, 2015 9:28 pm

Re: Crash out of loop with sudo halt = bad?

Tue Apr 11, 2017 10:34 pm

Excellent, thankyou :mrgreen:

Return to “Beginners”