How to get 2 CMD windows to 'talk' to each other?

时间:2017-06-12 16:54:36

标签: windows batch-file cmd ipc

I am probably missing the right vocabulary to talk about this problem more succinctly so excuse me if I'm a little wordy here. Under Windows 10 I have a program that runs inside a CMD command prompt It's an executable called OpenSim and it has it's own extensive command set, including 'shutdown', which initiates a graceful termination of the processes therein, closes SQL connections etc, then finally closes the CMD command window. I also have a CMD .bat file that is activated by my UPS when the power goes down that will of course open it's own window, and then does some housekeeping before closing down the hardware. One thing I want the .bat file to do is to somehow insert a 'shutdown'command into the other window's process. Is that possible? If so, how? Please assume I am a total newbie at this and you won't go far wrong. Thank you.

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 stackoverflow, 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

1 个答案:

答案 0 :(得分:1)

在=后添加双引号会将您的变量保存为您不想要的"C:\Opensim OSGrid\UPSFLAG.TXT"。而你想将它存储为C:\Opensim OSGrid\UPSFLAG.TXT,所以将报价移到lookforfile之前。

另外,您为该文件创建了一个变量,因此您也可以在删除中使用它。

最后,作为安全措施,总是在转到后退出。如果脚本中存在问题,这将确保系统存在,并且您可以确保在未计划的情况下不删除文件或关闭系统。

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