0

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

Toto
  • 89,455
  • 62
  • 89
  • 125
  • Check [this](https://unix.stackexchange.com/questions/107939/how-to-restart-the-python-script-automatically-if-it-is-killed-or-dies) thread – RJ Adriaansen Jul 03 '21 at 18:26
  • hey that doesn't answer my question i want to be able to still send an email(call a function) if my script is terminated by kill p{id} – dezlight Jul 03 '21 at 18:31
  • You don't. Functions don't exist separately from the process that executes them. What you want, I think, is a way to run the script in the background, with something that monitors the script to restart it if it ever exits. – chepner Aug 22 '21 at 14:56

1 Answers1

0

check this. By your description I assume you want to do something in response to kill signals.

schullzroll
  • 168
  • 13