Help me to initialize or trigger a python file to run in every 2 hrs?

Hi there,
I am new to Python.
I have a “ProcessAll.py” script which is schedule by zcron to run every 2 hrs. But currently the automatic refresh is having issue.

Is there any ways I can schedule the “ProcessAll.py” python script to run automatically from python script.

when I check on internet its show to schedule a job but not python file.
How can I use python script to schedule a python file to trigger.

Thanks

Usually I have better luck using something like cron or task scheduler (windows) to schedule scripts to run.

I don’t think there is a specific python thing that normally does that at least built-in.

A quick search found this module which may help: schedule — schedule 1.2.0 documentation

Using something like that, you would start a script on boot (or whenever) and schedule your task to call your script at the timeframe you need.

Though honestly it may make more sense to figure out why zcron isn’t triggering (and then if needed swap to something else).

I’ve never used zcron, so can’t comment on it directly.

To schedule your “ProcessAll.py” script to run automatically from another Python script, you can use the built-in schedule library in Python.

Hi Ahana,
Will you please help me with the script , how to use the built-in schedule and call or refer to any documents.
Thanks

When I first have a problem I search the internet for web pages. If you like videos than you can start on Youtube where the stuff is free. I like the videos by Indently. Try this: https://www.youtube.com/watch?v=FCPBG6NqMmQ&pp=ygUWcHl0aG9uIHNjaGVkdWxlIG1vZHVsZQ%3D%3D

So you would write a new program that does nothing but schedule tasks and run them. Call it “mycron.py” or “pycron.py”. :slight_smile:

When I get search results on Google or Youtube I set the filter to show show items from the past year so I don’t get examples that are deprecated or are no longer recommended.

Once you have seen the video then please try to write a program and figure it out, and also ask questions.

I personally takes lots of notes and write down examples of the programs that work, as I try them. I use Tiddlywiki for that on Tiddlyhost. You can put Tiddlywiki on a thumb drive, or put your wiki on Tiddlyhost and access it from any computer with internet.

1 Like

Ahana is possibly referring to the sched module from the stdlib:

Note that this assumes you’re running your program as a daemon: it is always there, but wakes up at the times you schedule to perform some task.

The core issue is that something has to implement the schedule. If you’re happy to have your Python script running in the background somewhere (I have several scripts I run this way - just sitting around waiting to do work) then sched can do this stuff for you.

If you don’t want that, you need another service to implement the schedule. On UNIXy systems (Linux, MacOS etc) that system is usually cron. I gather Windows has a system of a similar purpose (someone mentioned “task scheduler”? With these, you would put an entry into their system to invoke your script.

Hi Chuck,I tried this code

import schedule
import time
import CronJob

def job():
print(“I’m working…”)
schedule.every().day.at(“09:20”).do(job)
while True:
schedule.run_pending()
time.sleep(1)

But the job (CronJob) didn’t trigger. CronJob is my python script that I need to trigger every two hours a day.
My requirement is to trigger the CronJob.py to start at 09:20

I am trying the task scheduler , but its not triggering the python script

I don’t know enough about the Windows task scheduler to help you.

I am trying the task scheduler , but its not triggering the python script

Task scheduler can be tricky. I find the best way is to both add logging to a file (to your python script) and right click and click run now to test it.

Without logging, it’s going to be tough to figure out what happened. It says running, so in theory file writer started.

Ok, let’s start with something simple. I ran this and it worked, here’s what the program should look like:

# Call this file pycron.py. 
import schedule
import time
# import CronJob

def get_time() -> str:
    r'''Get current time and date and return as string.'''
    return time.strftime('%X (%d/%m/%y)')
    
def myjob():
    print("I’m working...",get_time())
    
# schedule.every().day.at(“09:20”).do(job)
schedule.every(5).seconds.do(myjob)

while True:
    schedule.run_pending()
    time.sleep(1)
   
  1. Remember to use a plain text editor, NEVER a word processor to edit program files. There are some good development tools with editors which are free like Visual Studio Code and Pycharm Community Edition for Python.
  2. Your code had extended ASCII double quotes which are not allowed in code and caused me errors, and a proper text editor would not put those in there.
  3. Also use code blocks when you show your code on this forum. The first line of a code block is 3 backticks in a row ``` on a line by themselves, and the last row is 3 backticks as well. If you don’t know what that is, please ask. There are no dumb questions here.
  4. I changed the function “job” to “myjob” so there are no crashes with other function names or reserved words, just in case. I do not have a photographic memory so I use caution when naming things.
  5. Can you get this program to work?
  6. Save this to a file called “pycron.py” and run it with “py pycron.py”. It worked for me.
  7. Are you using a virtual environment? This will become important with larger programs.
  8. Have you done a Python full tutorial yet? You need a good base of knowledge to start from. There are free ones on Youtube. I like the ones from Indently although I did my Python training on Udemy.com.

After doing a bit of research it looks like running another Python program is done via import. Try to get this next sample to work.

So I made a program “hello.py” in the same directory as pycron.py. This is probably important.

# My hello world python program.
def domain(a1, a2):
    print("Hello world")
    print("Arguments are", a1, a2)
    

if __name__=='__main__':
    domain()

And my calling program “pycron.py”.

# Call this file pycron.py. 
import schedule
import time
# import CronJob
import hello

def get_time() -> str:
    r'''Get current time and date and return as string.'''
    return time.strftime('%X (%d/%m/%y)')
    
def myjob():
    print("I’m working...",get_time())
    hello.domain('arg1', 'arg2') # This is from hello.py.
    
# schedule.every().day.at(“09:20”).do(job) # This has bad double quotes. 
schedule.every(5).seconds.do(myjob)

while True:
    schedule.run_pending()
    time.sleep(1) # Sleep 1 second.

Hi Chuck,
Thanks for you detailed explanation. it helped a lot.
I am using Visual Studio Code now.

The pycron.py script was running when I only having print(“I’m working…”,get_time()).

But I have call the 'CronJob.py like exec(open(path_scripts + ‘CronJob.py’).read())
and giving me error.

Any suggestion?

import schedule
import time
import datetime as dt
import os
import psutil

log_output_file = 'C:\\Python Scripts\\Production\\Python Scripts\\log.txt'
path_scripts = 'C:\\Python Scripts\\Production\\Python Scripts\\'

def log_file(message, log_output_file, file_attribute):
    f = open(log_output_file, file_attribute)
    f.write(message)
    f.close()    

now = dt.datetime.now()
print ("Current date and time : ")
print (now.strftime("%d-%m-%Y %H:%M:%S"))
log_file(now.strftime("%d-%m-%Y %H:%M:%S"), log_output_file, 'w')

# delete any excel workbooks that were opened during a reboot
# system account files
dir='C:\\Program Files\\Microsoft Office\\root\\Office16\\XLSTART'
for root, dirs, files in os.walk(dir):
    for name in files:

        os.remove(os.path.join(root, name))

# remove any instances of excel still running
for proc in psutil.process_iter():
    if proc.name() == "EXCEL.EXE":
        proc.kill()

def get_time() -> str:
    r'''Get current time and date and return as string.'''
    return time.strftime('%X (%d/%m/%y)')
    
def myjob():
    print("I’m working...",get_time())
    print('CronJob')  
    log_file('\n', log_output_file, 'a')
    log_file('CronJob', log_output_file, 'a')
    exec(open(path_scripts + 'CronJob.py').read())
    print('----------')

now = dt.datetime.now()
print ("Current date and time : ")
print (now.strftime("%d-%m-%Y %H:%M:%S"))
log_file('\n', log_output_file, 'a')
log_file(now.strftime("%d-%m-%Y %H:%M:%S"), log_output_file, 'a')

schedule.every(4).minutes.do(myjob)
while True:
      schedule.run_pending()
      time.sleep(1)
  

error message

Current date and time : 
24-04-2024 15:14:30
Current date and time : 
24-04-2024 15:14:30
I’m working... 15:18:30 (24/04/24)
CronJob
Obtain auth cookie
Obtain X-RequestDigest
Connect to SharePoint
Connect to SQLite Database
Create instance of Excel
Open Excel Template
Turn off calculations
Turn off alerts
Get License Data
Turn on calculations
Refresh workbook
Save Workbook
Upload file to SharePoint
Close out
Traceback (most recent call last):
  File "C:\Python Scripts\Production\Python Scripts\pycron.py", line 53, in <module>
    schedule.run_pending()
  File "C:\Python\Lib\site-packages\schedule\__init__.py", line 822, in run_pending
    default_scheduler.run_pending()
  File "C:\Python\Lib\site-packages\schedule\__init__.py", line 100, in run_pending
    self._run_job(job)
  File "C:\Python\Lib\site-packages\schedule\__init__.py", line 172, in _run_job
    ret = job.run()
  File "C:\Python\Lib\site-packages\schedule\__init__.py", line 693, in run
    ret = self.job_func()
  File "C:\Python Scripts\Production\Python Scripts\pycron.py", line 42, in myjob
    exec(open(path_scripts + 'CronJob.py').read())
  File "<string>", line 268, in <module>
  File "<string>", line 46, in close_out
NameError: name 'sqlite_connection' is not defined

Even this runs , just thinking how will the pycron.py gets trigger. Even if it close will it get trigger?
Is it possible that pycron.py gets trigger itself to run other python codes ?

Please show the error message as text, using the same formatting as your code block. This image is not useful: it repeats part of the code (which gives no useful information); we can’t copy text out of it for discussion; and it is cut off at the bottom (the most important part of the error message cannot be seen, because it was off the screen when you took the screenshot).

Even this runs , just thinking how will the pycron.py gets trigger. Even if it close will it get trigger?
Is it possible that pycron.py gets trigger itself to run other python codes ?

Use r-strings

log_output_file = 'C:\\Python Scripts\\Production\\Python Scripts\\log.txt'

When using Windows paths you must use R strings (raw strings) due to the backslashes.

Change all variables with a Windows path to use an r string like this:

log_output_file = r'C:\\Python Scripts\\Production\\Python Scripts\\log.txt'

Use variables for debugging

This exec(open(path_scripts + ‘CronJob.py’).read()) is not helpful for debugging. Again you have bad quotes around ‘CronJob.py’. Those are fancy quotes and Python does not like those. There are fancy quotes all over in your program code again. I make a variable for the full path like this.

# Make tpath so you can see the value in the pdb debugger. 
tpath = path_scripts + 'CronJob.py' # Use normal single quotes around CronJob.py
exec(open(tpath).read())

Use a debugger

You will need to learn to use a debugger, how to stop at a certain line, and to check the value of variables too. I use the pdb debugger. I also use Windows. I use the pdb debugger like this:

py -m pdb myprog.py

If you want to use a batch file and put in a path with spaces in Windows use double quotes around the path in your batch file:

py -m pdb "c:\python programs\myprog.py"

Questions for you

We need to get back to basics as it still looks like you are using a Word Processor to edit your program, or at least post code to this forum, not a text editor.

  1. Which text editor are you using to edit your programs?
  2. What is the exact process you use to copy and paste your program code into this forum? List every step. You should not be showing any fancy quotes when you paste code into this forum. Never use any word processor in any step as it will likely change normal quotes to fancy quotes. MS Word does this by default unless you change the settings.

Use r-strings

log_output_file = 'C:\\Python Scripts\\Production\\Python Scripts\\log.txt'

When using Windows paths you must use R strings (raw strings) due to the backslashes.

This isn’t strictly true. Because Windows uses backslashes as path
separators, and Python strings also use backslashes as escapes to
embed things line newlines (i.e. \n) you need to double every
backslash in a Windows path if you use normal Python quotes. So to
express a Windows path like:

 C:\Python Scripts\foo

you need a Python string like:

 'C:\\Python Scripts\\foo'

or you can use “raw strings”, where the Python quote is prefixed by an
r (for “raw”). So we could also write that Windows path as:

 r'C:\Python Scripts\foo'

with no annoying doubling of the backslashes.

Change all variables with a Windows path to use an r string like this:

log_output_file = r’C:\Python Scripts\Production\Python Scripts\log.txt’

This will be incorrect. You don’t want the doubled backslashes if you
use a “raw string”.

1 Like

Hi Chuck,

I am using IDLE
image

The first time when I run, the program is running successfully.
As you are mentioning about the path - then it would not run successfully at first point right

The error I am getting is from
schedule.run_pending()

Its something , the schedule is holding up thus.

Please let me know, if all the python file is close will it trigger the pycron.py by itself which we are trying to resolve .

Current date and time : 
29-04-2024 13:03:13
Current date and time : 
29-04-2024 13:03:13
I’m working... 13:07:13 (29/04/24)
CronJob
Obtain auth cookie
Obtain X-RequestDigest
Connect to SharePoint
Connect to SQLite Database
Create instance of Excel
Open Excel Template
Turn off calculations
Turn off alerts
Get License Data
Traceback (most recent call last):
  File "C:\Python Scripts\Production\Python Scripts\pycron.py", line 43, in <module>
    schedule.run_pending()
  File "C:\Python\Lib\site-packages\schedule\__init__.py", line 822, in run_pending
    default_scheduler.run_pending()
  File "C:\Python\Lib\site-packages\schedule\__init__.py", line 100, in run_pending
    self._run_job(job)
  File "C:\Python\Lib\site-packages\schedule\__init__.py", line 172, in _run_job
    ret = job.run()
  File "C:\Python\Lib\site-packages\schedule\__init__.py", line 693, in run
    ret = self.job_func()
  File "C:\Python Scripts\Production\Python Scripts\pycron.py", line 31, in myjob
    exec(open(tpath).read())
  File "<string>", line 197, in <module>
AttributeError: 'NoneType' object has no attribute 'Worksheets'