282013
 

转帖:http://www.blogfodder.co.uk/2012/4/20/win-2008-task-scheduler-with-return-code-1-0×1

今天在一台Windows服务器上部署一个计划任务的时候,遇到了这么一个问题。
要执行的任务很简单,即运行一个Python脚本,每小时执行一次。这种任务以前已经创建过很多个,一直运行良好。但是这次不同,任务计划程序每次都会迅速返回一个0×1的值然后退出任务,并且不认为任务失败。
查看脚本的执行log文件,发现直接就没有被执行过,而且手动执行可以得到完全正确的结果。由此可见,问题就出在了任务计划程序的配置上。
但是这个配置和以前都是完全一致的,可是就是始终不能执行。放Google搜,似乎中文的结果里面没有见到合适的解决方案,又换成英文再搜,终于有了答案。
原来,只需要在创建任务中的“操作”选项卡里面,新建操作,“程序或脚本”中只填脚本名称,在“起始于”里面填写脚本所在的路径。保存,生效!
虽然问题解决了,但是只能算是知其然,依旧不知其所以然。

附网贴原文:
Friday, April 20, 2012
I recently had the most infuriating issue with a scheduled task on a Win 2008 R2 server.

All I wanted it to do was run a .bat file once a day which fired a WGET script. If I logged into the server and clicked the bat file, it fired and triggered the WGET script and everything worked.

But whenever I tried to run it from the Task Scheduler (Either manually pressing run or letting it fire off the schedule), it failed and returned the code 0×1

I checked the history and log and it said it successfully completed with the following:

action “C:\Windows\SYSTEM32\cmd.exe” with return code 1

Great bit of information! Anyway, I spent quite some time changing permissions and users to no avail. I managed to make it work in the end, but its still not completely obvious to me why this makes a difference.

Instead of putting the full file path in the program/script textbox, use the Start in (Optional) field to put the folder that the .bat file is actually in – Like so:

Once you have done this, make sure you tick the ‘Run with highest privileges’ tick box

And that’s it. Its now returning the correct result code 0×0 and my script is running! Finally.