简体   繁体   English

taskkill:不同的行为控制台与脚本

[英]taskkill: different behaviour console vs. script

I am writing a small shutdown script for a java application. 我正在为Java应用程序编写一个小的关闭脚本。 I get the pid and then try to do the following: 我得到了pid,然后尝试执行以下操作:

taskkill /t /pid !pid!

If I am running the command from the console (typing it by hand) everything works fine. 如果我从控制台运行命令(手动键入命令),则一切正常。 I see the shutdown routine being executed in the java console and afterwards the java console is gone. 我看到关闭例程在Java控制台中执行,然后Java控制台消失了。 I sometimes see the windows "process not responding. kill now or wait" dialog after a few seconds but the dialog is automatically closed when the java program stops. 有时,几秒钟后,我会看到Windows“进程无响应。立即杀死或等待”对话框,但是当Java程序停止时,对话框自动关闭。

If i execute a bat file with the same command I immediately see the "Process not responding..." dialog. 如果我使用相同的命令执行bat文件,我会立即看到“进程无响应...”对话框。 I see the console output: "process was successfully shut down" but the java console is still open. 我看到控制台输出:“进程已成功关闭”,但Java控制台仍处于打开状态。 It didn't even initiate the shutdown sequence. 它甚至没有启动关机序列。

edit: the complete batch script 编辑:完整的批处理脚本

@echo off
setlocal enabledelayedexpansion
if exist .\lock (
set /p pid=< lock
taskkill /t /pid !pid!
) 

On the command line !pid! 在命令行上!pid! will normally not expanded, as delayed expansion is disabled and can only be enabled by the registry ey or with cmd /V:on . 通常不会扩展,因为已禁用延迟扩展,并且只能通过注册表ey或使用cmd /V:on来启用。

So at the command line !pid! 所以在命令行上!pid! will stay unchanged and in the batch it will be expanded to the content of the pid variable. 将保持不变,并在批处理中将其扩展为pid变量的内容。

Edit As you show the correct command on the cmd line, the command in the batch must be wrong. 编辑当您在cmd行上显示正确的命令时,批处理中的命令一定是错误的。

You should add an echo after the set/p line 您应该在set / p行之后添加回显

Echo pid="!pid!"

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

相关问题 Python - 在没有 taskkill /F 的情况下无法终止控制台脚本 - Python - Cannot terminate console script without taskkill /F TASKKILL特定的Python脚本 - TASKKILL specific Python script 动态与静态DLL链接是不同的 - Dynamic vs. Static DLL Linking is different Linux与Windows:控制台如何呈现unicode字符? - Linux vs. Windows: How does the console render unicode characters? 控制台中的处理标准输出与重定向时不同 - Process stdout different in console vs when redirected 32位与64位Windows版本的处理大小和行为差异 - Process sizes and differences in behaviour on 32bit vs. 64bit Windows versions Windows和Linux上的ImageMagick的“转换-棕褐色调”不同 - ImageMagick 'convert -sepia-tone' different on Windows vs. Linux 如何在批处理脚本中使用taskkill杀死子进程? - How can I kill child process using taskkill in a batch script? 综合列表-在批处理文件中运行代码与直接在控制台中输入代码 - Comprehensive list - Running code in batch file vs. entering code directly into console 从控制台运行和作为服务运行时,Java Service Wrapper出现问题 - Trouble with Java Service Wrapper when run from console vs. run as a service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM