I wrote a code that checks files and send mails if someone opens and access a specific file here is the code:
import atexit
import smtplib
from os import stat
import signal
email = "******"
password = "******"
message = "File has been touched you are busted"
f = "check.txt"
file_last_access_time = stat(f).st_atime
def sendemail():
s = smtplib.SMTP('smtp.gmail.com', 587)
s.starttls()
s.login(email, password)
s.sendmail(email, email, message)
print('Mail Sent')
s.quit()
return;
while True:
current_access_time = stat(f).st_atime
if file_last_access_time != current_access_time:
sendemail()
break
atexit.register(sendemail())
signal.signal(signal.SIGTERM, sendemail())
signal.signal(signal.SIGINT, sendemail())
I am running this .py script on Ubuntu through terminal. I am trying to find a way to make the code still send me the email even if I use kill{pid}. When I am killing the process, it doesn't send me an email would love to get some help to make it work