如何让 2 CMD windows 到 'talk' 彼此?
How to get 2 CMD windows to 'talk' to each other?
我可能缺少正确的词汇来更简洁地讨论这个问题,如果我在这里有点罗嗦,请原谅。在 Windows 10 下,我有一个在 CMD 命令提示符下运行的程序,它是一个名为 OpenSim 的可执行文件,它有自己的扩展命令集,包括 'shutdown',它启动其中进程的正常终止,关闭SQL 连接等,然后最终关闭 CMD 命令 window。我还有一个 CMD .bat 文件,它在断电时由我的 UPS 激活,当然会打开它自己的 window,然后在关闭硬件之前做一些内务处理。我想让 .bat 文件做的一件事是以某种方式将 'shutdown' 命令插入到另一个 window 的进程中。那可能吗?如果是这样,如何?请假设我是这方面的新手,你不会错得太远。谢谢。
EDIT It looks like creating a file to flag the closedown event taking place is the only (and I guess rather primitive) way to do this. So, building on what others have said in Whosebug, I have the following now. When I run it to test it waits - it doesn't. It runs right through to the end, running 'shutdown', even though the UPSFLAG.TXT file does not exist. What's going wrong?
echo Waiting for UPS Power Down Signal.
echo =================================
@ECHO OFF
SET LookForFile="C:\Opensim OSGrid\UPSFLAG.TXT"
:CheckForFile
IF EXIST %LookForFile% GOTO FoundIt
REM If we get here, the file is not found.
REM Wait 10 seconds and then recheck.
REM If no delay is needed, comment/remove the timeout line.
TIMEOUT /T 10 >nul
GOTO CheckForFile
:FoundIt
ECHO Found: %LookForFile%
rem Tidy up
del "C:\Opensim OSGrid\UPSFLAG.TXT"
shutdown
在 = 后添加双引号会将您的变量保存为您不想要的 "C:\Opensim OSGrid\UPSFLAG.TXT"
。相反,您想将其存储为 C:\Opensim OSGrid\UPSFLAG.TXT
,因此将引号移至 lookforfile 之前。
另外,您为文件创建了一个变量,所以您不妨在删除中使用它。
最后,作为安全措施,请始终在 goto 之后放置一个 exit。如果脚本中出现问题,这将确保系统存在,并且您可以确保您不会在计划之外删除文件或关闭系统。
echo Waiting for UPS Power Down Signal.
echo =================================
@ECHO OFF
SET "LookForFile=C:\Opensim OSGrid\UPSFLAG.TXT"
:CheckForFile
IF EXIST "%LookForFile%" GOTO FoundIt
REM If we get here, the file is not found.
REM Wait 10 seconds and then recheck.
REM If no delay is needed, comment/remove the timeout line.
TIMEOUT /T 10 >nul
GOTO CheckForFile
exit
:FoundIt
ECHO Found: %LookForFile%
rem Tidy up
del "%LookForFile"
shutdown
我可能缺少正确的词汇来更简洁地讨论这个问题,如果我在这里有点罗嗦,请原谅。在 Windows 10 下,我有一个在 CMD 命令提示符下运行的程序,它是一个名为 OpenSim 的可执行文件,它有自己的扩展命令集,包括 'shutdown',它启动其中进程的正常终止,关闭SQL 连接等,然后最终关闭 CMD 命令 window。我还有一个 CMD .bat 文件,它在断电时由我的 UPS 激活,当然会打开它自己的 window,然后在关闭硬件之前做一些内务处理。我想让 .bat 文件做的一件事是以某种方式将 'shutdown' 命令插入到另一个 window 的进程中。那可能吗?如果是这样,如何?请假设我是这方面的新手,你不会错得太远。谢谢。
EDIT It looks like creating a file to flag the closedown event taking place is the only (and I guess rather primitive) way to do this. So, building on what others have said in Whosebug, I have the following now. When I run it to test it waits - it doesn't. It runs right through to the end, running 'shutdown', even though the UPSFLAG.TXT file does not exist. What's going wrong?
echo Waiting for UPS Power Down Signal.
echo =================================
@ECHO OFF
SET LookForFile="C:\Opensim OSGrid\UPSFLAG.TXT"
:CheckForFile
IF EXIST %LookForFile% GOTO FoundIt
REM If we get here, the file is not found.
REM Wait 10 seconds and then recheck.
REM If no delay is needed, comment/remove the timeout line.
TIMEOUT /T 10 >nul
GOTO CheckForFile
:FoundIt
ECHO Found: %LookForFile%
rem Tidy up
del "C:\Opensim OSGrid\UPSFLAG.TXT"
shutdown
在 = 后添加双引号会将您的变量保存为您不想要的 "C:\Opensim OSGrid\UPSFLAG.TXT"
。相反,您想将其存储为 C:\Opensim OSGrid\UPSFLAG.TXT
,因此将引号移至 lookforfile 之前。
另外,您为文件创建了一个变量,所以您不妨在删除中使用它。
最后,作为安全措施,请始终在 goto 之后放置一个 exit。如果脚本中出现问题,这将确保系统存在,并且您可以确保您不会在计划之外删除文件或关闭系统。
echo Waiting for UPS Power Down Signal.
echo =================================
@ECHO OFF
SET "LookForFile=C:\Opensim OSGrid\UPSFLAG.TXT"
:CheckForFile
IF EXIST "%LookForFile%" GOTO FoundIt
REM If we get here, the file is not found.
REM Wait 10 seconds and then recheck.
REM If no delay is needed, comment/remove the timeout line.
TIMEOUT /T 10 >nul
GOTO CheckForFile
exit
:FoundIt
ECHO Found: %LookForFile%
rem Tidy up
del "%LookForFile"
shutdown