i posted question on forum without success, perhaps here can me?
i'm running raspberry pi v2 jessy.
there way python program detect "shutdown" or "restart" event has been initiated? have python program runs 24x7 in screen session window on 1 of raspberry pi's. have cron job reboots raspberry pi (shutdown -r 2) every couple days. when lose information in file buffers.
have read signal , have tried follow information on web. no success far. here code:
program runs in screen session/window , reacts expected cntl-c within screen session window , kill -15 <pid> main console window. however, when exit screen session, leaving program running, , enter sudo shutdown -r 2, pi reboots expected after 2 minutes, testsignal.txt file not show signal.sigterm event processed.
doing wrong? or better yet, how can trap shutdown event, initiated cron job, , close python program running in screen session gracefully? thanks....rdk
i'm running raspberry pi v2 jessy.
there way python program detect "shutdown" or "restart" event has been initiated? have python program runs 24x7 in screen session window on 1 of raspberry pi's. have cron job reboots raspberry pi (shutdown -r 2) every couple days. when lose information in file buffers.
have read signal , have tried follow information on web. no success far. here code:
code: select all
import time import signal import sys #import syslog log_file = '/usr/pgms/testsignal.txt' #define function process signal event def closeall(code, frame): print(time.strftime("%y-%m-%d %h:%m")+'\r\nshutdown detected') # syslog.syslog(syslog.log_crit, 'signal number:%d {%s}' % (code, frame)) f = open(log_file,'a') f.write(time.strftime("%y-%m-%d %h:%m")+'\r\nsignal code:' + str(code) + ' ') f.write('signal frame:' + str(frame)) f.write('\r\n') f.close() sys.exit(0) #register closeall function signal signal.signal(signal.sigterm,closeall) f = open(log_file,'a') f.write('\r\n') f.write('program started') f.write('\r\n') f.close() print(time.strftime("%y-%m-%d %h:%m")+'\r\nprogram running') try: while true: #write every 15 seconds time.sleep(15) f = open(log_file,'a') f.write(time.strftime("%y-%m-%d %h:%m")+'\r\nhello ') f.write('\r\n') f.close() # trap cntl-c keyboard entry except keyboardinterrupt: f = open(log_file,'a') f.write(time.strftime("%y-%m-%d %h:%m")+'\r\ncntl-c detected') f.write('\r\n') f.close()
doing wrong? or better yet, how can trap shutdown event, initiated cron job, , close python program running in screen session gracefully? thanks....rdk
my guess screen process gets term signal before script. send hup signals child processes. might worth shot try trapping these , see gets you. if doesn't work, try trapping each signal can think of , log signals have received. should know trap shutdown.
raspberrypi
Comments
Post a Comment