Windows 8.1 batch file - closing separate command window - windows

I'm trying to run a batch file that will close a different cmd window. I know I can do this:
TASKKILL /IM "myapplication.exe" /F
But whilst this does (obviously) kill the task, it doesn't close the cmd window myapplication.exe was running in. I cannot edit myapplication.exe or I would put an 'exit' on the last line.
Any help would be much appreciated.

Well, if your program was started from cmd, then obviously cmd is still running even if you kill or exit that application. To kill cmd as well you can apply the same command to a different program (in a quite obvious way).
However, this will also stop your batch file for equally obvious reasons.
Is there a particular need to run myapplication from cmd?

Related

Find PID of conhost.exe process for the current CMD window

I want to create a doskey command that closes the current CMD console window, without affecting any other open CMD console windows.
It has to work even when cmd.exe has been run (manually or by batch file) within that window. Ordinarily I would just use the exit command, but this does not close the window if other instances of cmd.exe have been run within that window.
I therefore decided to create a 'close' command that would always close the current window regardless of what had been run inside it. I chose the PID method of window detection (to avoid issues of windows with identical titles) and then used taskkill with /F and /T switches to kill the process. It worked fine, but it had the same problem as using the standard 'exit' command.
I ran tasklist and realised that the PID of the current CMD window changes whenever cmd.exe is run within it (you can see the multiple instances of cmd.exe listed, even though no new window has been opened) so using cmd.exe PID is not a usable method. However, each window does have its own conhost.exe process associated with it (regardless of the number of cmd instances) so killing the comhost process seems to be the best way to achieve what I want.
I tried it manually and it works exactly as I wanted.
Open one cmd console window, then run cmd.exe a couple of times within that window.
Open a second cmd console window, run cmd.exe a few times in that second window.
Use tasklist to find the PID of the conhost process for the window you
want to close.
Manually run:    taskkill /PID <PID of conhost process> /F   to kill the
selected conhost.
The above immediately closes the specified console window (and all of its related cmd.exe instances) while leaving the other console window alone.
I therefore just need to replicate the above process in a batch file so that I can use my doskey command to call that instead of my existing cmd PID checking code.
This is this code that I am using for current cmd PID detection (which I found online). I don't understand it (I'm a code newbie) but it works perfectly.
I then looked for method to determine the conhost PID based on the exiting cmd.exe PID of the current window, but I couldn't find anything except for this, which did not work (and which also caused various other problems with unrelated existing commands in my batch file).
Maybe there is an much simpler way of achieving my goal, but if killing conhost is the way to go then it seems to me that a modified version of the code below (to make it find the current conhost PID instead of the current cmd PID) would be the answer, because works reliably in a batch file very few lines of code.
set T=%TEMP%\sthUnique.tmp
wmic process where (Name="WMIC.exe" AND CommandLine LIKE "%%%TIME%%%") get ParentProcessId /value | find "ParentProcessId" >%T%
set /P A=<%T%
set PID=%A:~16%

Put server on heavy load for testing in Windows

Need to heavily exercise an eight-core server under Windows. Found a Linux exercising test at Put server on heavy load for testing and would like to perform a similar workout using command prompt commands.
Solved: I created a batch file DO.BAT which contains one line
dir /s /r /on & do ^z
and then from within a command prompt window entered
start do
which opened a new command prompt window and launched DO.BAT to loop until a Ctrl-Break is entered. Then, repeated that until many windows were running.
Many thanks to Bali C who mentioned the start command in How to run multiple DOS commands in parallel?

Batch error loop: Batch file execute by "mistake" another batch file when it opened

I have 2 batch files: kill_window1.bat & kill_host.bat. The content of bats are:
kill_window1.bat:
taskkill /f /im some_program.exe
kill_host.bat
taskkill /f /im taskhost.exe
The problem is that when I execute kill_window1.bat, it's like if I had executed kill_host.bat, and also, the process never ends, because cmd window never ends and still printing the line but nothing happens, just still open and my O.S. crash by more than 20 cmd's.
I'm using Windows 7 in a virtualbox.
Sorry if my English isn't perfect. I'm still learning ;)
PD: I'm killing taskhost.exe because sometimes use 100% CPU.
Try to use PsKill instead. Btw, it's strange that OS crashes. I hope the "some_program.exe" is not an OS process? Also, it's not a good idea to kill the taskhost.exe. Instead, try to discover which service loads CPU so high and stop only the service instead killing the host that can host several services.

Batch with "close cmd.exe and then re-execute" in timed loop

I have a batch file to run an application (I used to write some basic code in the 80s in DOS), and I need it to "re-execute" the application every x hours. The thing I cannot find is how to "close" (or stop) the application before running the code again, so it wont keep opening several cmd.exe windows with every loop. I am a total noob at coding but I know the principles of batch files
My actual .bat looks like this
setx some-minor-adjustments
application.exe -couple-of-variables
Then, it opens the usual cmd.exe window, and keeps running the application forever. What I need to do is, somehow, "refresh" (close and re-execute) the application automatically every x hours.
Closing the cmd.exe window is just fine to close the application, nothing more is needed in that sense (like data to save for example)
A friend suggested the cron command but it seems to be for other purposes. I also find over the Internet that I should use the start command to open the application in a new cmd.exe window, or something like that
Any ideas? Thanks in advance!
On Windows, the Task Scheduler is used instead of CRON.
Tasks in task scheduler can be set to shut down after a set time if you can be sure of the maximum duration.
You can also add an exit command after the main command if the main command can be assumed to finish (or spawns to a separate process).
:BEGIN
start /b mspaint
ping -n 6 localhost
tskill mspaint
GOTO BEGIN
This should work on all versions of Windows. Just keep in mind that you have to change the number 6 to the number of seconds of delay you want plus one i.e., the above ping statement will produce a delay of 5 seconds. Similarly, to produce a delay of 1 hour, you will have to write
ping -n 3601 localhost
Also make sure that the batch file is in the same folder as your executable otherwise you will have to specify full path.
Note: You can use mspaint.exe in the start command but for the tskill command, parameter should be only mspaint(no extensions). Using #echo off won't be of much help in this case as the ping requests are echoed back and can't be turned off. You can instead use a batch-to-exe converter and create a silent windowless executable. The -n parameter of ping can be very large but has to be in seconds.

cmd.exe doesn't terminate when using a .bat file

[Context: I'm trying to create a shortcut to a .bat file with a relative "Start in" path as roughly described here and here.]
cmd.exe supports the /c switch. According to the documentation, this should cause it to "carry out the command and then terminate."
But the switch seems to be ignored when the command is a .bat file.
For example, if you create a shortcut with the following Target (to a normal, non-bat command):
C:\Windows\System32\cmd.exe /c "START /d C:\temp\ notepad.exe test.txt"
Everything works as expected: Notepad opens and the console (shell) disappears. But if you replace the command above with a .bat file instead, like so:
C:\Windows\System32\cmd.exe /c "START /d C:\temp\ C:\test.bat"
(where test.bat contains only "notepad.exe test.txt") Notepad opens as before but the console sticks around like an unwanted friend. Why? And more to the point, How do I make it go away?
UPDATE: I know I can use wscript, as in this solution, but then I lose the option of having a custom icon (I'm stuck with the default .vbs icon).
The start command begins a new process for the batch file. The original cmd.exe then terminates, but leaves the new process, which hangs around because it's waiting for notepad.exe to terminate.
Change your bat file contents to:
start "" notepad.exe test.txt
Then your batch file will not wait for notepad to exit before continuing execution.
Another thing to try:
C:\Windows\System32\cmd.exe /c "START /d C:\temp\ C:\test.bat & exit"
The nuclear option would be to write a small program in the (compiled) language of your choice that launches the .bat file and then exits. Then you can give it a custom icon, and make it do whatever you like.
You might also take a look at Autoit from http://autoitscript.com as an alternative to batch. - the Run() command can do this kind of thing with better predictability. Since it makes an executable you can link this from a shortcut directly. You can also do a whole lot more of course, like run as a different user, insert delays or handle errors, that are hard to do with batch.
You don't need the full kit, just the Aut2EXE folder from the download will do.
BTW, build your exes without UPX compression as that leads to AV false positives.
I'm a little late but here is the answer.
The documentation for start states:
Syntax
START "title" [/D path] [options] "command" [parameters]
If command is an internal cmd command or a batch file then the command
processor is run with the /K switch to cmd.exe. This means that the
window will remain after the command has been run.
If start is used to execute a batch file, the opened cmd instance wont close.
You could also use call instead.
call C:\test.bat

Resources