Cum facem un DeadManSwitch in python

0

Buna ziua doamnelor si domnilor si bine v-am regasit. Ma numesc Zero Davila, iar astazi vom scrie un dead man switch in python. Un dead man switch e un process care daca utilizatorul nu  interactioneaza cu el intr-un anumit interval de timp, executa ceva, o comanda, un script, trimite un email, desi al nostru nu va face nimic din toate astea, doar va astepta input 24 de ore iar daca nu il va primi, procesul va fi terminat, daca il va primii, va avea o metoda care se apeleaza pe ea insasi si o va lua de la capat.

import signal
import time
import datetime
import threading

TIMEOUT = 1440 * 60 #24h

def interrupted(signum, frame):
print(‘Exit’)
signal.signal(signal.SIGALRM, interrupted)

def count(s):
current_time = datetime.datetime.now().time()
print (‘Activ de la : {}’.format(datetime.datetime.now().time()))
while True:
print (‘Ora : {}’.format(s))
s = s + 1
time.sleep(60 * 60) #1h

def i_input():
try:
print (‘Ai 24 de ore pentru a interactiona, sau procesul va inceta sa existe.’)
foo = input()
print (‘Input receptat la {}, resetare.’.format(datetime.datetime.now().time()))
i_input()
except:
return
signal.alarm(TIMEOUT)

threading.Thread(target = i_input).start()
countThread = threading.Thread(target=count, args=(0,));
countThread.start();

Leave A Reply

Your email address will not be published.