Hi,
I am new to Python. I have a “CronJob.py” script which is schedule by zcron to run every 2 hrs. But currently the automatic refresh is having issue.
I used schedule libraries but still unable to achieve my results.please suggest.
I tried this code in the test.py python script like below code where I import CronJob
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)
The above code is running with some error.
Current date and time :
24-04-2024 15:21:36
Current date and time :
24-04-2024 15:21:36
I’m working... 15:25:36 (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
Is it possible that CronJob.py gets trigger itself to run other python codes ?