简体   繁体   中英

How do I continue running a python script in the background

so I made a python script that will send a text to myself if I get an email from someone I specify. For example, in the script I input that if I get an email with "bobby", it will search the subject and body of the unread email. If I get an email with bobby in the subject or body, it will text me that I got an email from bobby.

I am using nohup python emtext_alerts.py & to run the script

As of right now, it will send me a text if ONE email with "bobby" in it comes. but if I get a second one, it will not text me again about it. How should I go about continuing to search for bobby related emails?

Here is my code:

from twilio.rest import Client
from imapclient import IMAPClient
import os

def main():
    checkEmail()

def checkEmail():
    server = IMAPClient('imap.gmail.com', use_uid=True)                                     #connect to IMAP server
    GMAIL_PASS = os.environ['GMAIL_PASS']                                                   #insert gmail password, may need to make an app account
    server.login('cscarlossamaniego@gmail.com', GMAIL_PASS)                                 #log in

    select_info = server.select_folder('INBOX')                                             #Select folder you want to search in
    topic_to_search = 'testing'                                                             #INPUT WHAT YOU WANT TO SEARCH FOR!
    unseen_messages_subject = server.search(['(UNSEEN)', 'SUBJECT', topic_to_search])       #Searching unread emails with topic to search in subject or body
    unseen_messages_body = server.search(['(UNSEEN)', 'TEXT', topic_to_search])

    if unseen_messages_subject:                                                             #send text if topic to search is in body/subject of unread email
        print("unread message about: '{0}'".format(topic_to_search))
        sendText(topic_to_search)
    elif unseen_messages_body:
        print("unread message about: '{0}'".format(topic_to_search))
        sendText(topic_to_search)

def sendText(topic_to_search):                                                      
    account_sid = os.environ['TWILIO_ACCOUNT_SID']                                          #created Twilio account with Accound SID and token
    auth_token = os.environ['TWILIO_AUTH_TOKEN']

    client = Client(account_sid, auth_token)                                                #create connection

    myTwilioNumber = os.environ['MY_TWILIO_NUMBER']                                         #input twilio number
    myCellPhone = os.environ['MY_CELL_PHONE_NUMBER']                                        #input ur phone number

    #sends message
    message = client.messages.create(body="Email about '{0}'".format(topic_to_search), from_=myTwilioNumber, to=myCellPhone)  
    print(message.sid)

if __name__ == '__main__':
    main() 

Please let me know!

If you're using Linux nohup:

nohup /path/to/test.py &

This will keep the python (or any script) running in the background even if the terminal session ends.

Windows you can use pythonw.exe to run something.

pythonw.exe test.py

All of this assumes your script will loop. It sounds like your script is running one time; even if you have the task run in the background it will still terminate. You need to add logic

I would build a loop in your script, a while loop like

condition = 0
while condition = 0:
    Run Your Code

Replace Run Your Code with your block of code you want to execute.

This would keep your task running in the background, while ensuring the Python is still checking your email

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM