Programs blocking the current shell

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
misol101
Posts: 475
Joined: 02 May 2016 18:20

Programs blocking the current shell

#1 Post by misol101 » 24 Jun 2016 09:30

I have run into something confusing. Some programs, when run from a batch program, seems to block forever, even after the program quits! I am then forced to close down the cmd window, Ctrl-C/Z/break do not work.

For me, this seems to be the case with Chrome. I don't know if it's the same with all versions, if it's just on my computer etc.

Note: this only happens when called from a bat file, when executed from command line only (3) and (6) freezes!

Here are the commands in test.bat I used to test this (the numbers are just for clarity, obviously, and I try one line per run by commenting out the rest):

Code: Select all

::"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" (1)
::call "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" (2)
::cmd /C "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" (3)
::start C:\Program Files (x86)\Google\Chrome\Application\chrome.exe (4)
::start "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" (5)
::start cmd /C "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" (6)
start /B cmd /C "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" (7)


Case (1): Chrome opens, and even though I close down Chrome, the cmd line is stuck!
Case (2): Same as 1
Case (3): Same as 1
Case (4): Stupid one, since I have spaces in the path it just fails, but...
Case (5): A new cmd window is opened, and nothing else happens (why is it like this?)
Case (6): A new cmd window is opened, Chome runs, the new window freezes even after exiting Chrome (at least the original window doesnt freeze)
Case (7): The only one that "works", but has many undesired effects (for one, the "invisible" process is most likely frozen, and second if I run something else than Chrome this way, e.g. a bat file, things turn quite strange (key input doesnt work etc)). And I want to be able to run anything with this method, not just Chrome or exe files.

I should also note that every second time when running Chrome this way, absolutely nothing happens! (i.e. Chrome does not run, so no freeze, but also no Chrome)

What is going on here? I am confused. Can anybody reproduce this?

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Programs blocking the current shell

#2 Post by Compo » 24 Jun 2016 09:36

What happens with:

Code: Select all

start "" "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" [8]
start "" "%Program Files(x86)%\Google\Chrome\Application\chrome.exe" [9]
start %Program Files(x86)%\Google\Chrome\Application\chrome.exe [10]
start chrome.exe [11]

misol101
Posts: 475
Joined: 02 May 2016 18:20

Re: Programs blocking the current shell

#3 Post by misol101 » 24 Jun 2016 09:52

8, 9 and 11 all have the same behaviour:

No new window opens, No more freeze! Still, every second time I run Chrome, nothing happens. But the freeze is gone, thanks!

(10 produced a faulty line)

Not sure I get what's going on here. Unfortunately it's still not quite what I want, because when I use these to run a bat file, a new window is opened. I would like to stay in the same window, unless the program itself opens new windows.
Last edited by misol101 on 24 Jun 2016 10:03, edited 1 time in total.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Programs blocking the current shell

#4 Post by aGerman » 24 Jun 2016 10:02

It MUST be normal behavior that calling an external programm blocks the batch execution. Think about command tools like findstr.exe or choice.exe. They would be completely useless otherwise.

For calling a program asynchronuously you have to use START. Although the syntax is that the first parameter enclosed into quotes will be interpreted as window title (even if this only effects programs that run in a console window). For GUI programms just pass an empty string as Compo said.

Regards
aGerman

misol101
Posts: 475
Joined: 02 May 2016 18:20

Re: Programs blocking the current shell

#5 Post by misol101 » 24 Jun 2016 10:07

aGerman wrote:It MUST be normal behavior that calling an external programm blocks the batch execution. Think about command tools like findstr.exe or choice.exe. They would be completely useless otherwise.


Of course, but when the external program quits, the batch execution should resume, right!? That's what was not happening in the case of Chrome. It seems Compo's lines fixes this though.

Is there any way I can make the line Compo used (e.g.[8]) NOT open a new cmd window if I run a bat file or an exe file with console output?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Programs blocking the current shell

#6 Post by aGerman » 24 Jun 2016 10:13

Well as far as I remember Chrome runs every tab in a new process. No idea what happens then...

Use option /b for programs/scripts that run in a console window. That option forces the attaching of the process to the same console window.

Regards
aGerman

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Programs blocking the current shell

#7 Post by Compo » 24 Jun 2016 10:17

misol101 wrote:Is there any way I can make the line Compo used (e.g.[8]) NOT open a new cmd window if I run a bat file or an exe file with console output?

As already answered

Code: Select all

start /b "" "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" [8]
start /b "" "%Program Files(x86)%\Google\Chrome\Application\chrome.exe" [9]
start /b chrome.exe [11]

misol101
Posts: 475
Joined: 02 May 2016 18:20

Re: Programs blocking the current shell

#8 Post by misol101 » 24 Jun 2016 10:27

Compo wrote:As already answered

Code: Select all

start /b "" "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" [8]
start /b "" "%Program Files(x86)%\Google\Chrome\Application\chrome.exe" [9]
start /b chrome.exe [11]


What I mean is, if I run e.g:

start /b "" "whatever.bat"

then a new cmd line window is opened, and similar if I run an exe file which produces console output. And I would like that not to happen (I only want a new window to open if the program I run opens new window(s) itself)

I mean, yes, obviously I can do: "whatever.bat" , without the start /b. But I need this for the general case. I don't think I can know if e.g. an exe file will produce a new window or not, so I don't know if to run "file.exe" or start /b "" "file.exe"

misol101
Posts: 475
Joined: 02 May 2016 18:20

Re: Programs blocking the current shell

#9 Post by misol101 » 24 Jun 2016 10:30

So to explain: I do *not* want to run the program asynchronuously. What I want is how I *thought* "call" or "cmd /C" would behave, i.e. run the program, and whenever it quits, resume batch operation. The problem is that after Chrome quits, the batch file for some reason is still frozen, and I was looking at alternatives or an explanation for this behavior.

And using start /B (which I did in case 7 above) has side effects (for console programs/bat files), such as the program running no longer listening to key input

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Programs blocking the current shell

#10 Post by Compo » 24 Jun 2016 10:38

misol101 wrote:And using start /B (which I did in case 7 above) has side effects (for console programs/bat files), such as the program running no longer listening to key input
[7] however ran cmd.exe with a command parameter and didn't include the additional empty doublequotes

misol101
Posts: 475
Joined: 02 May 2016 18:20

Re: Programs blocking the current shell

#11 Post by misol101 » 24 Jun 2016 10:42

Compo wrote:
misol101 wrote:And using start /B (which I did in case 7 above) has side effects (for console programs/bat files), such as the program running no longer listening to key input
[7] however ran cmd.exe with a command parameter and didn't include the additional empty doublequotes


True, but doing it your way (as in [8]):

start /B "" "file.bat"

has the same side effects (and the side effect is that my external exe file, which runs getch to get key input, no longer gets any input).
Last edited by misol101 on 24 Jun 2016 10:47, edited 1 time in total.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Programs blocking the current shell

#12 Post by aGerman » 24 Jun 2016 10:44

It's hard for me to even understand.

Code: Select all

start "" /b /wait "whatever application.ext"

... starts a new (!) console script/application instance in the same window. Because of /wait it runs synchronously.

I don't know what happens with Chrome (I don't use it and I won't use it). As I said it starts several chrome.exe processes.

Regards
aGerman

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Programs blocking the current shell

#13 Post by Compo » 24 Jun 2016 10:46

misol101 wrote:
Compo wrote:
misol101 wrote:And using start /B (which I did in case 7 above) has side effects (for console programs/bat files), such as the program running no longer listening to key input
[7] however ran cmd.exe with a command parameter and didn't include the additional empty doublequotes


True, but doing it your way (as in [8]):

start /B "" "file.bat"

has the same side effects.
You cannot change from running an external executable to running a batch file by file association and expect the advice to match your original posts and their responses!

misol101
Posts: 475
Joined: 02 May 2016 18:20

Re: Programs blocking the current shell

#14 Post by misol101 » 24 Jun 2016 11:00

Compo wrote:You cannot change from running an external executable to running a batch file by file association and expect the advice to match your original posts and their responses!


Sorry, I have probably been unclear. I tried to "simplify" things at the start, probably a bad idea :)

I'll try again:

I have a batch file called listb.bat, which is a file explorer for cmd. When you press 'i' in the program, whatever file is currently selected is launched using " cmd /C "file.ext" "

This has worked just fine so far! I.e. a bat file runs in the same window, a console exe runs in the same window, a gif file launches the associated viewer etcetc. And when the program finishes, the file explorer batch resumes.

THEN, I pressed 'i' on a html file, and Chrome starts. I read the file, then shut down Chrome, BUT... the batch file explorer is now frozen! So this is the origin of the problem, and my 7 attempts above was just me trying to find some way around it.


start "" "file.exe" works, which is good, but it opens a new window for bat files (and exe files that run in console mode). Which I would like to avoid (I agree I did not specify this in the initial question, sorry).

start /b seems to cause all kinds of strange behavior, but I will try to examine that a bit more.
Last edited by misol101 on 24 Jun 2016 12:00, edited 4 times in total.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Programs blocking the current shell

#15 Post by aGerman » 24 Jun 2016 11:09

What happens if you only close the tab with your html file instead of the whole Chrome window?

Post Reply