任务调度程序,终止 Windows 中启动的程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22912440/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 11:29:50  来源:igfitidea点击:

Task scheduler, terminate started program in windows

windowstaskscheduler

提问by Johannes

At my work we have a set up with the task scheduler periodically starting a java program to read mails. the task is scheduled to run every minute and it calls a .bat file which starts the java program.

在我的工作中,我们设置了任务调度程序,定期启动一个 Java 程序来读取邮件。该任务计划每分钟运行一次,它调用一个 .bat 文件来启动 java 程序。

Now the problem. Once in a month or so the jave.exe process doesn't end properly, so the next minute when it tries to run I get:

现在的问题。一个月左右一次,jave.exe 进程没有正常结束,所以在它尝试运行的下一分钟我得到:

Task Scheduler failed to start "\XXX Jobs" task for user "NT AUTHORITY\System". Additional Data: Error Value: 2147750687.

任务计划程序无法为用户“NT AUTHORITY\System”启动“\XXX Jobs”任务。附加数据:错误值:2147750687。

And then I get that message every minute until I terminate the java.exe from the task manager.

然后我每分钟都会收到这条消息,直到我从任务管理器中终止 java.exe。

Now my question, in task scheduler there are some options to choose. Under settings there is "If the task is already running, then the following rule applies" If I then choose "Stop the existing instance" Will this stop the java.exe or just the task? Or is there a better way.

现在我的问题是,在任务调度程序中有一些选项可供选择。在设置下有“如果任务已经在运行,则适用以下规则”如果我选择“停止现有实例”这会停止 java.exe 还是只停止任务?或者,还有更好的方法。

Some advice would be welcome.

欢迎提供一些建议。

采纳答案by Lo?c MICHEL

"Stop the existing instance" : The Task Scheduler service will stop the instance of the task that is already running, and run the new instance of the task.

“停止现有实例”:Task Scheduler 服务将停止已经运行的任务实例,并运行该任务的新实例。

that means it will kill the process that the scheduler has launched, cmd.exein your case, as you told us your program is started from within a batch.

这意味着它将终止调度程序启动的进程,cmd.exe在您的情况下,正如您告诉我们您的程序是从批处理中启动的。

Now, i'm not familiar with java but i guess that stopping your batch will kill the java process that where launched if it is not started as a service.

现在,我不熟悉 java,但我想停止批处理会杀死启动的 java 进程,如果它不是作为服务启动的。

The scheduler will then run another cmd process and execute your batch once again

然后调度程序将运行另一个 cmd 进程并再次执行您的批处理

回答by Andrew

At the end of your batch file kill the task.

在批处理文件的末尾终止任务。

taskkill /im java.exe

java.exe could be whatever process you are planning on killing. You can add multiple lines of tasskill in a batch file to kill multiple processes at a time.

java.exe 可以是您计划杀死的任何进程。您可以在批处理文件中添加多行 tasskill 以一次杀死多个进程。

taskkill /im java.exe
taskkill /im explorer.exe
taskkill /im svhost.exe

回答by dvasanth

Better solution would be fix the java hang. As the process starts every minute then in your java program should have the code to automatically exit if it takes more than a minute to complete the task. Another way would be in the batch file you can kill the process after 1 minute. Use the taskkill /im to kill the process.

更好的解决方案是修复 java 挂起。由于该过程每分钟启动一次,如果完成任务需要一分钟以上,则在您的 Java 程序中应该具有自动退出的代码。另一种方法是在批处理文件中,您可以在 1 分钟后终止该进程。使用 taskkill /im 来终止进程。