简体   繁体   English

通过执行 pscp 的 windows 任务调度程序运行 python 脚本时出现问题

[英]Problems running python script by windows task scheduler that does pscp

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 several scheduled tasks.我有一个 Windows 2008 服务器,我正在其上运行多个计划任务。 One of those tasks is a python script that uses pscp to log into a linux box, checks for new files and if there is anything new, copies them down to a local directory on the C: drive.其中一项任务是 python 脚本,它使用 pscp 登录到 linux 框,检查新文件,如果有任何新文件,将它们复制到 Z0D61F8370CAD1D412F80B84D143E125 上的本地目录:驱动器。 I've put some logging into the script at key points as well and I'm using logging.basicConfig(level=DEBUG) .我也在关键点将一些日志记录到脚本中,并且我正在使用logging.basicConfig(level=DEBUG)

I built the command using a variable, command = 'pscp -pw xxxx name@ip:/ c:\local_dir' and then I use subprocess.call(command) to execute the command.我使用变量构建命令, command = 'pscp -pw xxxx name@ip:/ c:\local_dir'然后我使用 subprocess.call subprocess.call(command)来执行命令。

Now here's the weird part.现在这是奇怪的部分。 If I run the script manually from the command line, it works fine.如果我从命令行手动运行脚本,它工作正常。 New files are downloaded and processed.新文件被下载和处理。 However, if the Task Scheduler runs the script, no new files are downloaded.但是,如果任务计划程序运行脚本,则不会下载新文件。 The script is running under the same user, but yet yields different results.该脚本在同一用户下运行,但产生不同的结果。

According to the log files created by the script and on the linux box, the script successfully logs into the linux box.根据脚本创建的日志文件,在linux盒子上,脚本成功登录到linux盒子。 However, no files are downloaded despite there being new files.但是,尽管有新文件,但不会下载任何文件。 Again, when I run it via the command line, files are downloaded.同样,当我通过命令行运行它时,会下载文件。

Any ideas?有任何想法吗? suggestions, alternative methods?建议,替代方法?

Thanks.谢谢。

I had the same issue when trying to open an MS Access database on a Linux VM. 尝试在Linux VM上打开MS Access数据库时遇到了同样的问题。 Running the script at the Windows 7 command prompt worked but running it in Task Scheduler didn't. 在Windows 7命令提示符下运行脚本有效但在任务计划程序中运行它没有。 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)). 解决方案是让Task Scheduler运行cmd作为Program / Script,参数为/ c python C:\\ path \\ to \\ script.py (在Add arguments(optional)下)。

I can't tell you why this works but it solved my problem. 我无法告诉你为什么这样有效,但它解决了我的问题。

You can use the windows Task Scheduler, but make sure the "optional" field "Start In" is filled in. 您可以使用Windows任务计划程序,但请确保填写“可选”字段“开始”。

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: 在任务计划程序应用程序中,添加一个操作,指定您的python文件运行“doSomeWork”并使用包含该文件的目录填写Start in(可选)输入。例如,如果您有一个python文件:

C:\pythonProject\doSomeWork.py

You would enter: 你会输入:

Program/Script: doSomeWork.py

Start in (optional): C:\pythonProject 

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. 在测试中,我发现使用子进程的任何类型的调用在任务调度程序中运行时都会停止python脚本,但在命令行上运行时工作正常。

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脚本,如下所示:

python test.py >> log.txt

I run the script through the batch file both on command line and through task scheduler. 我在命令行和任务调度程序中通过批处理文件运行脚本。

Brad's answer is right. 布拉德的回答是正确的。 Subprocess needs the shell context to work and the task manager can launch python without that. 子进程需要shell上下文才能工作,任务管理器可以在没有它的情况下启动python。 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. 另一种方法是创建一个由任务调度程序启动的批处理文件,调用python c:\\ path \\ to \\ script.py等。唯一的区别是,如果你遇到一个有调用的脚本对于os.getcwd(),你将始终获得脚本所在的根,但是当你从任务调度程序调用cmd时,你会得到其他东西。

Last edit - start 最后编辑 - 开始

After experiments... If you put there full path to python program it works without highest privileges (as admin). 经过实验...如果你把完整的路径放到python程序,它没有最高权限(作为管理员)。 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 . 我所做的是更改了任务设置:选中Run with highest privileges And task started to work perfectly while running python [script path] . 在运行python [script path]时,任务开始完美运行。 But keep in mind, that title contains "Administrator: " at the begining... always... 但请记住,该标题在开头包含“管理员:”...总是......

PS Thanks guys for pointing out that subprocess is a problem. PS谢谢你们指出子进程是一个问题。 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. 当一个脚本从Windows任务计划程序运行时,我有类似的问题,而另一个脚本没有。 Running cmd with python [script path] didn't work for me on Windows 8.1 Embedded x64. 使用python [script path]运行cmd在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. 希望我的回答有助于某人。 ;) ;)

Create a batch file add your python script in your batch file and then schedule that batch file .it will work . 创建批处理文件在批处理文件中添加python脚本,然后安排该批处理文件。它将起作用。 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 : 示例:假设您的python脚本位于文件夹c:\\ abhishek \\ script \\ merun.py中,首先您必须通过cd命令转到目录。所以您的批处理文件将是:

cd c:\\abhishek\\script python merun.py cd c:\\ abhishek \\ script python merun.py

it work for me . 它对我有用。

只是留给后代:通过在我的 .bat 和 .py 脚本中随处使用 UNC (\\10.x.xx.xx\\Folder\\xxx) 路径而不是分配给驱动器的字母( \\K:\\文件夹\\xxx)

I had this issue before.我以前有这个问题。 I was able to run the task manually in Windows Task Scheduler, but not automatically.我能够在 Windows 任务计划程序中手动运行该任务,但不能自动运行。 I remembered that there was a change in the time made by another user, maybe this change made the task scheduler to error out.我记得另一个用户的时间发生了变化,也许这个变化使任务调度程序出错。 I am not sure.我不确定。 Therefore, I created another task with a different name, for the same script, and the script worked automatically.因此,我为同一个脚本创建了另一个名称不同的任务,该脚本自动运行。 Try to create a test task running the same script.尝试创建一个运行相同脚本的测试任务。 Hopefully that works!希望这有效!

For Anaconda installation of python in windows below solution worked for me对于 Anaconda 在 windows 中安装 python 以下解决方案对我有用

  1. create a batch file with below "C:\Users\username\Anaconda3\condabin\activate" && python "script.py" && deactivate使用以下 "C:\Users\username\Anaconda3\condabin\activate" && python "script.py" && deactivate 创建一个批处理文件
  2. Setup task to run this batch file设置任务以运行此批处理文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM