为什么非交互式批处理脚本认为我按下了 control-C? [英] Why does a non-interactive batch script think I've pressed control-C?

查看:24
本文介绍了为什么非交互式批处理脚本认为我按下了 control-C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以当突然出现在输出日志中时,我的批处理脚本运行良好:

So my batch script is ticking along nicely when suddenly this appears in the output log:

21:27:13.99 c:appsw7lab-scripting>some-command
Error 3221225786
^CTerminate batch job (Y/N)? 

然后脚本就停止了.

批处理脚本在会话 0 中运行,所以我知道它没有收到真正的 control-C,而且我的代码都没有调用 GenerateConsoleCtrlEvent 所以不可能是这样.唯一的线索是 some-command 当时正在与一个交互式应用程序通信,并且 那个 应用程序的控制台收到了一个 control-C.预期的行为是 some-command 显示另一个应用程序的退出代码,然后以相同的代码退出.如果错误没有停止,批处理脚本会适当地处理错误.

The batch script is running in session zero, so I know it didn't receive a real control-C, and none of my code calls GenerateConsoleCtrlEvent so that can't be it. The only clue is that some-command was communicating with an interactive application at the time, and that application's console received a control-C. The expected behaviour was for some-command to display the other application's exit code, then exit with the same code. The batch script would have dealt with the error appropriately, if it hadn't stopped dead.

这是怎么回事?

推荐答案

这里的神奇之处在于退出代码 3221225786,也就是 0xC000013A 或 STATUS_CONTROL_C_EXIT.

The magic here is in exit code 3221225786, aka 0xC000013A or STATUS_CONTROL_C_EXIT.

交互式应用程序收到了一个 control-C,但没有抓住它,所以正如预期的那样,它被 STATUS_CONTROL_C_EXIT 中止了.some-command 应用程序正确地将此报告为远程应用程序的退出代码,并将其传递回批处理脚本.

The interactive application received a control-C, and didn't catch it, so as expected, it was aborted with STATUS_CONTROL_C_EXIT. The some-command application correctly reported this as the remote application's exit code, and passed it back to the batch script.

我没有意识到的是,cmd.exe 通过检查子进程是否返回 STATUS_CONTROL_C_EXIT 来检测批处理脚本中的 control-C.因此,通过返回此错误代码,我无意中停止了批处理脚本.

What I hadn't realized was that cmd.exe detects control-C in a batch script in exactly this way, by checking whether a child process returns STATUS_CONTROL_C_EXIT. So by returning this error code I was inadvertently stopping the batch script.

这可以用一个简单的批处理脚本来演示:

This can be demonstrated with a simple batch script:

cmd /c exit 3221225786
echo hello

运行时产生

C:working	est>cmd /c exit 3221225786
^CTerminate batch job (Y/N)?

这篇关于为什么非交互式批处理脚本认为我按下了 control-C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆