通过执行 pscp 的 Windows 任务调度程序运行 python 脚本的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20196049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Problems running python script by windows task scheduler that does pscp
提问by user1070061
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. 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. I've put some logging into the script at key points as well and I'm using logging.basicConfig(level=DEBUG).
我有一台运行多个计划任务的 Windows 2008 服务器。其中一项任务是使用 pscp 登录 linux box 的 python 脚本,检查新文件,如果有新文件,将它们复制到 C: 驱动器上的本地目录。我也在关键点将一些日志记录到脚本中,并且我正在使用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(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. However, no files are downloaded despite there being new files. Again, when I run it via the command line, files are downloaded.
根据脚本创建的日志文件,在linux box上,脚本成功登录linux box。但是,尽管有新文件,但不会下载任何文件。同样,当我通过命令行运行它时,会下载文件。
Any ideas? suggestions, alternative methods?
有任何想法吗?建议,替代方法?
Thanks.
谢谢。
采纳答案by Brad Posthumus
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.
尝试在 Linux VM 上打开 MS Access 数据库时遇到了同样的问题。在 Windows 7 命令提示符下运行脚本有效,但在任务计划程序中运行它没有。使用任务计划程序,它会找到数据库并验证它是否存在,但不会返回其中的表。
The solution was to have Task Scheduler run cmdas the Program/Script with the arguments /c python C:\path\to\script.py(under Add arguments (optional)).
解决方案是让任务计划程序将cmd作为程序/脚本运行,参数为/c python C:\path\to\script.py(在添加参数(可选)下)。
I can't tell you why this works but it solved my problem.
我不能告诉你为什么这有效,但它解决了我的问题。
回答by Steve
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.
我有一个类似的问题。在测试中,我发现任何类型的 subprocess 调用在任务调度程序中运行时都会停止 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.
我通过命令行和任务调度程序通过批处理文件运行脚本。
回答by citizen2191629
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.
布拉德的回答是正确的。子进程需要 shell 上下文才能工作,任务管理器可以在没有它的情况下启动 python。另一种方法是制作一个批处理文件,该文件由调用 python c:\path\to\script.py 等的任务调度程序启动。唯一的区别是,如果您遇到一个有调用的脚本到 os.getcwd() ,您将始终获得脚本所在的根目录,但是当您从任务调度程序调用 cmd 时,您会得到其他东西。
回答by Erkin Djindjiev
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:
在 Task Scheduler 应用程序中,添加一个操作,指定您的 python 文件运行“doSomeWork”,并使用包含该文件的目录填写 Start in(可选)输入。例如,如果您有一个 python 文件:
C:\pythonProject\doSomeWork.py
You would enter:
你会输入:
Program/Script: doSomeWork.py
Start in (optional): C:\pythonProject
回答by Constantine Ketskalo
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:
实验后......如果你把python程序的完整路径放在那里,它可以在没有最高权限的情况下工作(作为管理员)。意思是这样的任务设置:
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...
我所做的是更改了任务设置:已选中Run with highest privileges。并且任务在运行时开始完美运行python [script path]。但请记住,该标题在开头包含“管理员:”...总是...
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. ;)
PS 感谢大家指出子进程是一个问题。这让我想到了任务设置。当一个脚本从 Windows 任务计划程序运行时,我遇到了类似的问题,而另一个没有。python [script path]在 Windows 8.1 Embedded x64 上运行 cmd对我不起作用。不知道为什么。可能是因为必须在路径中有空格和引号问题。希望我的回答对某人有所帮助。;)
回答by abhishek
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 :
创建一个批处理文件,在批处理文件中添加您的 python 脚本,然后安排该批处理文件。它会工作。示例:假设您的 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 .
它对我有用。

