简体   繁体   中英

Running python script in Service account by using windows task scheduler

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

Having the same bizar issue. Works like a charm when running as user. With a windows task the glob query returns no results.

Edit: was using a network share by its mapping name. Only works when using the full UNC path (including the server name).

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