Code: Select all
30 8 * * * /bin/kill -USR1 `cat /var/run/processname.pid`
00 9 * * * /bin/kill -USR2 `cat /var/run/processname.pid`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)I'd do it the first way. Use cron to start the process, then something inside the program to decide when to stop.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.
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() 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.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.