Keep Windows command window open - windows

I'm running a command through the Windows shell- an existing command window (cmd.exe). When I execute the command, the window closes, even though it's a freestanding window not tied to the command.
How can I keep the window open to see the output?

You can't simply start a child cmd session because it'll share same window and if your custom tool actively closes its window (I wonder why) then it'll close your console and output will disappear.
There isn't much you can do if a program want to close console window but you can at least save its output to a file (to be inspected later with type). If you're working with that console and you don't want to close it then you can use start cmd to execute it in a new console window. Like this:
start cmd /c tool -args ^> output.txt
tool output will be available in output.txt after it finished.

It appears that the executable is closing the command window. Here is what you could try, may work. open a command shell. In the shell issue "cmd" and open another command shell. Run your executable in the newly opened command shell. You nested cmd will be exited, but you may still be able to see some of the output of your executable.

Related

Does Windows log the commands given to cmd.exe?

I know that I can retrieve the commands I've given in a command window up to a certain point. cmd.exe seems to have a certain cache size and once it's exhausted the commands overflow. When I close the command window everything is deleted, right? Or wrong? Does Windows still know which commands I gave and can I retrieve them?
If I run a program in a command window and the program calls cmd.exe to run a command, can I see this command somehow?
Type doskey /?.
So doskey /history.
It is not a log but a command recaller.
echo %cmdcmdline% shows Cmd's command line. Else you can read the command line in task manager. Right click the headings in Detail tab. Choose Command Line.
This lists all variables https://winsourcecode.blogspot.com/2019/05/listenvironmentexe-list-system-user.html

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%

Close the command prompt window programmatically

I have a set of commands in batch file which is used to run the JAR file.It is working perfectly.
After running the program, the command prompt window doesn't get closed. If I manually close the window, then the application closes as well.
So I want to close the command window without affecting the application.
thanks in advance.
Either use exit or make a new method to go to and have nothing in it. The exit way is better though. :)
My psychic debugging powers tell me you're launching the JAR file using a java command instead of a javaw command.
Use javaw instead and the command window will close when the batch file exits.

how to make my console in python not to close?

I'm making a application in python from Windows. When I run it in the console, it stops, shows an error, and closes. I can't see the error becase its too fast, and I can't read it. I'm editing the code with IDLE (the program that came with python when I instaled it), and when I run it with the python shell, there are no errors. I would run it from IDLE, but when I use the console, it has more features.
I don't know why this is happening. I need your help.
Run the program from an already-open terminal. Open a command prompt and type:
python myscript.py
For that to work you need the python executable in your path. Just check on how to edit environment variables on windows, and add C:\PYTHON26 (or whatever directory you installed python to).When the program ends, it'll drop you back to the CMD windows prompt instead of closing the window.Add code to wait at the end of your script. Adding ...
raw_input()
... at the end of the script makes it wait for the ENTER key. That method is annoying because you have to modify the script, and have to remember removing it when you're done.
Run your program from a Windows command prompt. That will not automatically close when the program finishes.
If you run your program by double-clicking on the .py file icon, then Windows will close the window when your program finishes (whether it was successful or not).
Create a text file in the program directory i.e. wherever your script is located. Change the extension to .bat for example text.bat. Then edit the text file and write:
python main.exe
pause
Now you can run the program without typing into the command console by double clicking the bat file, and the console window will not close.

Stop vim from flashing command prompt in Windows?

Vim under win32 opens a command prompt (vimrun.exe actually, which opens in a terminal window) on every external command, silent or not. Yes, the terminal closes automatically, but it is still quite annoying.
This makes plugins that make extensive use of external commands, such as syntastic (it runs a command on buffer open/save), a real pain.
Is there some way to fix this behavior? What I want is for the terminal to open only for non-silent commands.
Rather than just ![windows command] you might try:
!start /min [windows command]
Alternatively, if you define a shortcut to a windows app you can click on the shortcut's properties and set it up to run as 'Minimized' rather than as 'Normal'
In both cases above an app button will show up on taskbar as the app is opened, as there would be for any minimized application. But it's less intrusive than having an actual window open.
NOTE The !start command solution runs the command asynchronously, resumes Vim immediately without waiting for the command to complete, which may often not be what you want. In that case the use of shortcut set up to run as minimized is better solution.
Just to bring closure: I wrote a replacement runner utility for Vim on Windows that doesn't open a visible command prompt. Here it is: vimrun-silent.

Resources