Running python script in Service account by using windows task scheduler

微笑、不失礼 提交于 2021-02-11 16:43:38

问题


NOTE 1- All files are running using cmd in my profile and fetching correct results.But not with the windows task scheduler.

**> NOTE 2- I finally got a lead that glob.glob and os.listdir is not

working in the windows task scheduler in my python script in which I am making a connection to a remote server, but it is working in my local using cmd and pycharm.** **

print("before for loop::", os.path.join(file_path, '*')) 
print(glob.glob( os.path.join(file_path, '*') ))
for filename in glob.glob( os.path.join(file_path, '*') ):
print("after for loop")

** While running above .py script I got: before for loop:: c:\users\path\dir\* While executing print(glob.glob( os.path.join(file_path, '*') )) giving "[]" and not able to find why?

I followed this StackOverflow link for setting up Windows Scheduler for python by referring to MagTun comment:Scheduling a .py file on Task Scheduler in Windows 10

  1. Currently, I am having scheduler.py which is calling the other 4 more .py files.

  2. When I try to run Scheduler.py from Windows Task SCHEDULER,

It runs Scheduler.py and then after 1 minute it runs all other 4 .py files and exit within a seconds. Not giving any output in elastic search.

I used this for cmd script:

 @echo off
cmd /k "cd /d D:\folder\env\Scripts\ & activate &  cd /d D:\folder\files & python scheduler.py" >> open.log

timeout /t 15

In this above cmd command, It is not saving anything in open.log when running with windows task scheduler.

Script with multiple .py subprocess schedulers is like this:

from apscheduler.schedulers.blocking import BlockingScheduler
import datetime
from subprocess import call
from datetime import datetime
import os
from apscheduler.schedulers.blocking import BlockingScheduler

def a():
    call(['python', r'C:\Users\a.py'])

def b():
    call(['python', r'C:\Users\b.py'])

def c():
    call(['python', r'C:\Users\c.py'])

def d():
    call(['python', r'C:\Users\d.py'])

if __name__ == '__main__':
    scheduler = BlockingScheduler()
    scheduler.add_job(a, 'interval', minutes=1)
    scheduler.add_job(b, 'interval', minutes=2)
    scheduler.add_job(c, 'interval', minutes=1)
    scheduler.add_job(d, 'interval', minutes=2)
    print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))
    try:
        scheduler.start()
        print("$$$$$$$$$$$$$$$$$$")
    except (KeyboardInterrupt, SystemExit):
        print("****#####")
        pass

来源:https://stackoverflow.com/questions/61512201/running-python-script-in-service-account-by-using-windows-task-scheduler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!