5

I have got the below scrip and i want it to run every 30 minutes can someone point me in the right direction on how to do this.

I have searched for existing questions like this but don't seem to find any think that will work with my script but don't know if that's me being dumb.

My script goes to different positions on my screen clicks and then does a screen shot and then sends me the image to my gmail account.

import pyautogui
import time
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email import encoders
import os


time.sleep(5)
pyautogui.PAUSE = 1
pyautogui.moveTo(922,134)
pyautogui.click()
pyautogui.PAUSE = 1
pyautogui.moveTo(178,277)
pyautogui.click()
pyautogui.PAUSE = 1
pyautogui.moveTo(178,297)
pyautogui.click()
pyautogui.PAUSE = 1
pyautogui.moveTo(178,315)
pyautogui.click()
pyautogui.PAUSE = 1
pyautogui.screenshot('web.png')

pyautogui.PAUSE = 5

gmail_user = "user@gmail.com"
gmail_pwd = "password"

to = "user@gmail.com"
subject = "Report"
text = "Picture report"
attach = 'web.png'

msg = MIMEMultipart()

msg['From'] = gmail_user
msg['To'] = to
msg['Subject'] = subject

msg.attach(MIMEText(text))

part = MIMEBase('application', 'octet-stream')
part.set_payload(open(attach, 'rb').read())
encoders.encode_base64(part)
part.add_header('Content-Disposition',
   'attachment; filename="%s"' % os.path.basename(attach))
msg.attach(part)

mailServer = smtplib.SMTP("smtp.gmail.com", 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(gmail_user, gmail_pwd)
mailServer.sendmail(gmail_user, to, msg.as_string())
# Should be mailServer.quit(), but that crashes...
mailServer.close()
Slam
  • 8,112
  • 1
  • 36
  • 44
scott.turner
  • 69
  • 1
  • 2
  • 6

5 Answers5

5

Use windows schtasks:

schtasks /create /sc minute /mo 30 /tn "PyAutoGUI Task" /tr "python <path to script>"
Totoro
  • 867
  • 9
  • 10
0

Your loop should look like this:

while '1' == '1':
    '''
    Your script that should loop here
    '''
    time.sleep(1800)
NaruS
  • 168
  • 1
  • 15
0

Let's assume the filename for your original script is testfile.py

Make another file in the same directory as the first one with this code:

import time,os
while 1:
    time.sleep(1800)
    os.system("start python testfile.py")

Running this script will run your other file every 30 minutes regardless of how long the script takes to run!

Qwerty
  • 1,252
  • 1
  • 11
  • 23
  • 1
    being picky: if the task itself lasts 20 minutes, that won't run the script every 30 minutes but every 50 minutes. – Jean-François Fabre Feb 15 '18 at 15:32
  • @Jean-FrançoisFabre I've changed it to run every 20 minutes regardless of the time it takes to run – Qwerty Feb 15 '18 at 15:35
  • 1
    This might not be a feasible solution. Even though the python program sleeps for the time specified, its just you are asking the python to not perform any operations, which means Python compiler is still utilizing your processor. What if the program execution lasts only for 10 secs ? You are sacrificing the processor for 29 mins 50 secs. – ExtractTable.com Feb 15 '18 at 15:40
  • In any form of Unix, the CPU is released during sleep() calls. Pretty sure this is the case with Python's time.sleep(). Please update if I'm wrong! – Brian C Nov 23 '21 at 01:09
0
  1. Wrap the program (make sure to supply the credentials by setting environment variables, if needed)
  2. Create a batch file which commands the processor to run python program
  3. Create a task in Windows Scheduler to trigger the batch file
ExtractTable.com
  • 762
  • 10
  • 20
0

I believe time.sleep(), as stated in the rest of the answers would occupy the processor even when not in use.

I had a similar requirement where I had to run a section of code, but also connect with server every 30 minutes otherwise it would time out.

This is what I did

import time
start = time.time()
process_time = 0
max_processing_time = 20000
sleep_time = 1800

while process_time < max_processing_time:
    if (time.time() - start) > sleep_time:
        start = time.time()
        service = service_connect() ## Connect to server every 30 minutes
    
    ## Otherwise keep on running this code
    function_which_I_want_to_run_continously()

In your sccenario, you can insert your section of code within the 'if' block. You can set max_processing_time and sleep_time as per your requirement.

Syed Md Ismail
  • 827
  • 1
  • 9
  • 13
  • Looks to me like this version uses the CPU continuously; [as far as I can tell, Python's time.sleep() does release the CPU](https://stackoverflow.com/questions/17075788/python-is-time-sleepn-cpu-intensive) – Brian C Nov 23 '21 at 01:36