Problems running python script by windows task scheduler that does pscp

后端 未结 6 1888
轮回少年
轮回少年 2020-12-08 23:16

Not sure if anyone has run into this, but I\'ll take suggestions for troubleshooting and/or alternative methods.

I have a Windows 2008 server on which I am running s

相关标签:
6条回答
  • 2020-12-08 23:31

    Brad's answer is right. Subprocess needs the shell context to work and the task manager can launch python without that. Another way to do it is to make a batch file that is launched by the task scheduler that calls python c:\path\to\script.py etc. The only difference to this is that if you run into a script that has a call to os.getcwd() you will always get the root where the script is but you get something else when you make the call to cmd from task scheduler.

    0 讨论(0)
  • 2020-12-08 23:32

    Create a batch file add your python script in your batch file and then schedule that batch file .it will work . Example : suppose your python script is in folder c:\abhishek\script\merun.py first you have to go to directory by cd command .so your batch file would be like :

    cd c:\abhishek\script python merun.py

    it work for me .

    0 讨论(0)
  • 2020-12-08 23:44

    I'm having a similar issue. In testing I found that any type of call with subprocess stops the python script when run in task scheduler but works fine when run on the command line.

    import subprocess
    
    print('Start')
    test = subprocess.check_output(["dir"], shell=True)
    print('First call finished')
    

    When run on command line this outputs:

    Start
    First call finished
    

    When run from task scheduler the output is:

    Start
    

    In order to get the output from task scheduler I run the python script from a batch file as follows:

    python test.py >> log.txt
    

    I run the script through the batch file both on command line and through task scheduler.

    0 讨论(0)
  • 2020-12-08 23:50

    Last edit - start

    After experiments... If you put there full path to python program it works without highest privileges (as admin). Meaning task settings like this:

    program: "C:\Program Files\Python37\python.exe"
    arguments: "D:\folder\folder\python script.py"
    

    I have no idea why, but it works even if script uses subprocess and multiple threads.

    Last edit - end

    What I did is I changed task settings: checked Run with highest privileges. And task started to work perfectly while running python [script path]. But keep in mind, that title contains "Administrator: " at the begining... always...

    P.S. Thanks guys for pointing out that subprocess is a problem. It made me think of task settings. I had similar problem when one script is running from Windows Task Scheduler, and another one doesn't. Running cmd with python [script path] didn't work for me on Windows 8.1 Embedded x64. Not sure why. Probably because of necessity to have spaces in path and issue with quotes. Hope my answer helps someone. ;)

    0 讨论(0)
  • 2020-12-08 23:54

    You can use the windows Task Scheduler, but make sure the "optional" field "Start In" is filled in.

    In the Task Scheduler app, add an action that specifies your python file to run "doSomeWork" and fill in the Start in (optional) input with the directory that contains the file.. So for example if you have a python file in:

    C:\pythonProject\doSomeWork.py
    

    You would enter:

    Program/Script: doSomeWork.py
    
    Start in (optional): C:\pythonProject 
    
    0 讨论(0)
  • 2020-12-08 23:58

    I had the same issue when trying to open an MS Access database on a Linux VM. Running the script at the Windows 7 command prompt worked but running it in Task Scheduler didn't. With Task Scheduler it would find the database and verify it existed but wouldn't return the tables within it.

    The solution was to have Task Scheduler run cmd as the Program/Script with the arguments /c python C:\path\to\script.py (under Add arguments (optional)).

    I can't tell you why this works but it solved my problem.

    0 讨论(0)
提交回复
热议问题