简体   繁体   中英

How can I run commands on existing cmd window from another cmd window?

I have one command prompt instance running (lets call it A). A takes 10 mins time to load (Loading includes intialisation, running few pre requisite commands etc). I have one more command prompt instance running (lets call it B). B is just simple command prompt.

Now I want to run a command on A instance from B instance. Something like, eg

cmd B:> call A.exe "dosomething"

Now here I want "dosomething" to be ran on A cmd instance.

I tried with wmic .

C:\Users\dbe1>wmic process where 'caption like "%cmd.exe"' get  name, processid
Name     ProcessId
cmd.exe  2628
cmd.exe  17620

I got process IDs.Now I can call to treminate, create etc on this process IDs.

wmic process where processid="18172" call terminate

It works just fine, since terminate method is understood by windows.I want to call " dosomething " here which is my custom requirement.

Is it possible?

Read Win32_Process Methods .

The Win32_Process class exposes the following methods:

  • AttachDebugger method: launches the currently registered debugger for the process;
  • Create method: creates a new process;
  • GetAvailableVirtualSize method: retrieves the currently size in bytes of the free virtual address space available to the process;
  • GetOwner method: retrieves the user name and domain name under which the process is running;
  • GetOwnerSid method: retrieves the security identifier (SID) for the owner of the process;
  • SetPriority method: attempts to change the execution priority of the process;
  • Terminate method: terminates the process and all of its threads.

You have to have something running in A capable of being told what to do, but you can do it: in a cmd script, A.cmd, running in cmd.exe A, add the following:

waitfor dosomething

then in cmd.exe B, type the following:

waitfor /SI dosomething

A.cmd (and any other instances or running cmd scripts executing waitfor dosomething at that time) will execute the next line of script: this could either be something hard-coded into A.cmd, or you could do something like the following:

B.cmd:

echo @echo Hello World > dosomething.cmd
waitfor /SI dosomething

A.cmd:

waitfor dosomething
if EXIST dosomething.cmd call dosomething.cmd

One drawback to this is that each individual cmd.exe is not multi-threaded, but you can just overcome that by running multiple instances (ie, multi-process instead of multi-thread); you'd also have to put each instance into its own folder so it would have its own copy of dosomething.cmd to manipulate.

B.cmd:

echo @echo Hello A instance 1 > \A1\dosomething.cmd
echo @echo Hello A instance 2 > \A2\dosomething.cmd

REM both instances will be signaled by the following line
waitfor /SI dosomething

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM