User avatar
Douglas6
Posts: 4874
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: How do I cleanly stop a python routine.

Tue Jan 27, 2015 8:45 pm

Lots of ways to do that. You could get the time of day in your loop, compare if it's later than the time you want to stop, and if so 'break' out of the loop. Or you could run a cron script with a 'kill' command to abruptly end your program. In that case you should have a signal handler to clean things up.

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

Re: How do I cleanly stop a python routine.

Tue Jan 27, 2015 9:29 pm

In cron you can use

Code: Select all

30 8 * * * /bin/kill -USR1 `cat /var/run/processname.pid`
00 9 * * * /bin/kill -USR2 `cat /var/run/processname.pid`
In your python process

Code: Select all

import signal

def sigUSR1handler(signum, stack):
  # do stuff when signalled with USR1

def sigUSR2handler(signum, stack):
  # different stuff to do for USR2 signal

signal.signal(signal.SIGUSR1, sigUSR1handler)
signal.signal(signal.SIGUSR2, sigUSR2handler)
The hard part is capturing the process id (pid) when the process starts and writing it somewhere where your cronjobs can read it. If you do that from an /etc/init.d script then it's done for you.
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.

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: How do I cleanly stop a python routine.

Tue Jan 27, 2015 9:34 pm

Douglas6 wrote:Lots of ways to do that. You could get the time of day in your loop, compare if it's later than the time you want to stop, and if so 'break' out of the loop. Or you could run a cron script with a 'kill' command to abruptly end your program. In that case you should have a signal handler to clean things up.
I'd do it the first way. Use cron to start the process, then something inside the program to decide when to stop.

gordon77
Posts: 5077
Joined: Sun Aug 05, 2012 3:12 pm

Re: How do I cleanly stop a python routine.

Thu Jan 29, 2015 4:51 pm

You could just start and stop it from python at appropriate times,

To start

Import subprocess
Import os

p=subprocess.Popen(rpistr,shell=True, preexec_fn=os.setsid) where rpistr is your raspistill command etc eg "raspistill -t 0 -tl 0 etc.

To stop it use. os.killpg(p.pid, signal.SIGTERM)

Example

Code: Select all

import RPi.GPIO as GPIO
import time
import subprocess, os
import signal
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO_switch = 24  # pin 18
GPIO.setup(GPIO_switch,GPIO.IN)

print "  Press Ctrl & C to Quit"

try:
    
   run = 0
   while True :
      if GPIO.input(GPIO_switch)==0 and run == 0:
         print "  Started"
         rpistr = "raspistill -o /run/shm/test.jpg -t 0 -tl 0  "
         p=subprocess.Popen(rpistr,shell=True, preexec_fn=os.setsid)
         run = 1
         while GPIO.input(GPIO_switch)==0:
             time.sleep(0.1)
      if GPIO.input(GPIO_switch)==0 and run == 1:
         print "  Stopped " 
         run = 0
         os.killpg(p.pid, signal.SIGTERM)
         while GPIO.input(GPIO_switch)==0:
             time.sleep(0.1)
       

except KeyboardInterrupt:
  print "  Quit"
  GPIO.cleanup()   
To get the hour you could use

now = datetime.datetime.now()
hour = now.hour
if hour > 8 and hour < 20:
Etc.....

User avatar
joan
Posts: 14960
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: How do I cleanly stop a python routine.

Thu Jan 29, 2015 5:43 pm

DougieLawson wrote:In cron you can use

Code: Select all

30 8 * * * /bin/kill -USR1 `cat /var/run/processname.pid`
00 9 * * * /bin/kill -USR2 `cat /var/run/processname.pid`
...
The hard part is capturing the process id (pid) when the process starts and writing it somewhere where your cronjobs can read it. If you do that from an /etc/init.d script then it's done for you.
You could use /usr/bin/killall as pretty much a drop in replacement for kill. It is slightly friendlier in that it uses process names rather than process ids.

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: How do I cleanly stop a python routine.

Fri Feb 06, 2015 3:07 pm

I would assume the first is your actual Python script and the second is another thread or child process started from the first? So killing the first/main one should probably kill the child process.

Could you not just have one Python script that starts at boot, runs forever (if not stopped manually or through GPIO or something external), only takes the photos and emails during the specified times and just sleeps when you don't want the photos - why make it so complicated with multiple processes and external tools/commands (if not for learning and/or distributed processing reasons)?
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

Return to “Python”