简体   繁体   English

将批处理文件作为计划任务运行会不会表现出应有的作用?

[英]Running a batch file as a scheduled task does not behave as it should?

I have a jar file, which uses jdbc to connect to a sql db and add a new record. 我有一个jar文件,该文件使用jdbc连接到sql db并添加新记录。 To execute this with an arg I put the cmd line command in a batch file to run it. 要使用arg执行此操作,我将cmd line命令放在批处理文件中以运行它。

Now if I manually click and run the batch file it works fine and I can see the new record in my sql database, but I have created a scheduled task to run the batch file for me once day which it seems do successfully but there are no new records added to the database. 现在,如果我手动单击并运行该批处理文件,它可以正常工作,并且可以在我的sql数据库中看到新记录,但是我创建了一个计划的任务来为我运行一次该批处理文件,看起来好像成功了一次,但是没有新记录添加到数据库中。

Any ideas on why this happens? 关于发生这种情况的任何想法?

The batch file is just one line, as it seemed to work when I ran it? 批处理文件仅占一行,因为我运行它时似乎可以工作? :

java -Dvar=daily -jar SQL_JDBC_Bandon.jar java -Dvar =每日-jar SQL_JDBC_Bandon.jar

I post this as an answer because it would look ugly in a comment. 我将其发布为答案,因为它在评论中看起来很难看。

To have a log of executions to compare to the task execution log of scheduled tasks create a batch similar to this. 要使执行日志与计划任务的任务执行日志进行比较,请创建与此类似的批处理。

@echo off
echo JDBC executed %DATE% %TIME% >> C:\temp\logs\jdbctask.log
C:
cd \PathToYourJar
C:\Programs\Java\bin\java -version >> C:\temp\logs\jdbctask.log
Dir C:\Programs\Java\bin\java.* >> C:\temp\logs\jdbctask.log
C:\Programs\Java\bin\java -Dvar=daily -jar SQL_JDBC_Bandon.jar >> C:\temp\logs\jdbctask.log

This makes sure that you don't rely on PATH or JAVA_HOME variables that might not be set for the user you run the batch in. Please adjust the paths to your needs. 这样可以确保您不依赖运行批处理的用户可能未设置的PATH或JAVA_HOME变量。请根据需要调整路径。 If this still fails, post the results of the log file. 如果仍然失败,请发布日志文件的结果。

I figured it out a while back, but all I did was put these 2 lines in a .bat file and it works without any issues as a scheduled task: 我不久前就发现了,但是我所做的只是将这两行放在一个.bat文件中,并且它作为预定任务可以正常工作:

cd C:\Users\SQLService\Desktop\ScadaCalcs
java -Dvar=daily -jar SQL_JDBC_Bandon.jar

One thing to note is that the jar and script are both in the ScadaCalcs directory, but I had to cd an absolute path for it to work. 需要注意的一件事是jar和脚本都在ScadaCalcs目录中,但是我必须使用cd绝对路径才能工作。

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

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