简体   繁体   English

将批处理作为计划任务令人困惑的问题

[英]Confusing issue with batch as a Scheduled Task


I have a DOS batch file to run on a daily basis. 我有一个DOS批处理文件,可以每天运行。
Something similar like - 类似的东西-

@ECHO ON
SET COMMON_LIB=commons-io-1.3.1.jar;
SET AR_CLASS_PATH=%CLASSPATH%%COMMON_LIB%

java -cp %AR_CLASS_PATH% -Xms128m -Xmx256m FileCreating

PAUSE

When I run the batch file directly, ie double cliking on the .bat file, it runs fine, the command window opens up and executes all the required commands( note the PAUSE ). 当我直接运行批处理文件时,即在.bat文件上双击,它运行良好,命令窗口打开并执行所有必需的命令(请注意PAUSE )。
But when I schedule a daily task, I see the status as Running. 但是,当我安排日常任务时,我会看到状态为“正在运行”。 Also, when I right click on the task, it gives me an option to end the task(when status is Running ) but I cant see the command window and so I cant make out if it has been processed or the error it has generated. 另外,当我右键单击该任务时,它为我提供了一个结束任务的选项(状态为Running ),但我看不到命令​​窗口,因此无法确定它是否已处理或产生了错误。
And so, cant understand if the error is in classpath or my java code or somewhere else. 因此,无法理解错误是在类路径中还是在我的Java代码中或其他地方。

The environment is Windows Server 2003 R2 EE, SP2. 环境是Windows Server 2003 R2 EE,SP2。 The user has Admin priviledges. 用户具有管理员权限。
I checked but there is no file of Schedlgu.txt in WINDOWS\\Tasks dir. 我检查了一下,但是WINDOWS\\Tasks目录中没有Schedlgu.txt文件。
One thing I noticed was the CLASSPATH value had no reference to the jdk/bin, can that be a issue? 我注意到的一件事是CLASSPATH值没有引用jdk / bin,这可能是个问题吗? Please advise. 请指教。

EDIT 编辑

Just to simplify things, I commented the java command for the bat file to do almost nothing then set some variables and then pause to keep the window open. 为了简化操作,我对bat文件的java命令进行了注释,使其几乎不执行任何操作,然后设置了一些变量,然后暂停以保持窗口打开。 Still no success. 仍然没有成功。

Scheduled tasks do not necessarily open a visible window. 计划任务不一定打开可见的窗口。

In my experience, if there is an active desktop session for the relevant user at the time the scheduled tasks starts, then it will usually create a window in that session. 以我的经验,如果计划任务开始时相关用户有一个活动的桌面会话,那么通常会在该会话中创建一个窗口。 If there is more than one such session, it will select one of them, but I am not sure how the choice is made. 如果此类会话不止一个,它将选择其中一个,但是我不确定如何做出选择。 If there is no such active session, then no window will be created at all. 如果没有这样的活动会话,那么将根本不会创建任何窗口。

It seems most likely that the job is running through and then stopping at the PAUSE command, with no way to receive input that would make it continue. 作业很可能正在运行,然后停止在PAUSE命令上,而没有办法接收使作业继续进行的输入。 This is difficult to verify since PAUSE is an internal command and will not show up as a separate process in Task Manager or Process Explorer. 这是很难验证的,因为PAUSE是内部命令,不会在任务管理器或进程资源管理器中显示为单独的进程。 But if you can verify that there is no java process running, this would support the belief that the java portion of the batch has completed and it is stuck on the PAUSE. 但是,如果您可以验证没有Java进程正在运行,则可以认为该批处理的Java部分已经完成并且卡在了PAUSE上。

In general, when running a batch file as a scheduled task, it is wise to redirect both standard output and standard error to a file so you can look for errors after the job has completed. 通常,将批处理文件作为计划任务运行时,将标准输出和标准错误都重定向到文件是明智的,这样您就可以在作业完成后查找错误。

Well, after some trial and error, I was finally able to get my scheduled task to run. 好吧,经过一番尝试和错误之后,我终于能够使计划的任务运行。
As suggested by Dave, I had the std out redirected to a text file, so that I can see what errors are generated. 根据Dave的建议,我将标准输出重定向到了一个文本文件,以便可以看到生成了哪些错误。 But the text file came out blank and the task was run. 但是文本文件显示为空白,任务已运行。
So I added a java -version 所以我加了一个java -version

@ECHO ON

SET COMMON_LIB=commons-io-1.3.1.jar;
SET AR_CLASS_PATH=%CLASSPATH%%COMMON_LIB%

java -version

java -cp %AR_CLASS_PATH% -Xms128m -Xmx256m FileCreating

and still no result, even now the text file was blank. 仍然没有结果,即使现在文本文件还是空白。

Then, I created a simple HelloWorld program and added this java command to the bat file 然后,我创建了一个简单的HelloWorld程序,并将此java命令添加到bat文件中

@ECHO ON

SET COMMON_LIB=commons-io-1.3.1.jar;
SET AR_CLASS_PATH=%CLASSPATH%%COMMON_LIB%

java -version

java HelloWorld > C:\ChechHW.txt

java -cp %AR_CLASS_PATH% -Xms128m -Xmx256m FileCreating

The CheckHW.txt had the output text, I had given in the HelloWorld program. CheckHW.txt具有我在HelloWorld程序中给出的输出文本。

So now all these added to the confusion and annoyance. 所以现在所有这些都增加了混乱和烦恼。

Interestingly and surprisingly, the issue was the CLASSPATH variable I am using to set the classpath with the java command. 有趣且令人惊讶的是,这个问题是我用来通过java命令设置类路径的CLASSPATH变量。
I dont know how and importantly why it started working after I removed the %CLASSPATH% from 我不知道从我删除%CLASSPATH%后它如何开始工作,以及为何重要

SET AR_CLASS_PATH=%CLASSPATH%%COMMON_LIB% 

The bat file in my question was and is working on a Windows XP system without this change. 我的问题中的bat文件过去一直并且正在Windows XP系统上运行,而没有进行此更改。
I wonder some setting on the Windows 2003 Server related to Java class path was not allowing it to process. 我想知道Windows 2003 Server上与Java类路径相关的某些设置不允许它处理。

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

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