15

Is there any equivalent in Windows to Unix Shell's "exec"? Basically, I need to avoid forking a new process, so that input/output pipes are preserved, as well as process id.

Edit:

So, here's my problem. I have a process A that starts a script, and this script ends by executing a process B. I need for A to get all of B's output, as well as be able to kill B by killing the process it has spawned (the script).

On Unix, executing B with exec does that job.

Daniel C. Sobral
  • 5,713
  • 6
  • 34
  • 48
  • There is no equivalent, at least not using standard Windows commands and programs. – John Gardeniers Sep 24 '11 at 08:47
  • If you're trying to run an executable, rather than another batch file, there is no (supported/realistic) way of doing this. However, input/output pipes are normally inherited by applications running in a batch file. If you can describe why you want to do this, we may be able to suggest alternatives. – Harry Johnston Sep 25 '11 at 20:41
  • Process A should already get all of B's output, unless the script overrides this. Is this not working? – Harry Johnston Sep 26 '11 at 02:17
  • It is possible using the Win32 API to arrange for a child process to die when the parent process does, however there is no built-in command-line option AFAIK. If I provide some source code (for Visual C++) are you able to build it? – Harry Johnston Sep 26 '11 at 02:26
  • @Harry I'm forking the script from Java, so, if the idea is for process A to use that Win32 API, I'm afraid that's unlikely to work, though I'm thankful for the effort. Better just to explain it isn't possible, and I'll accept that as the answer. – Daniel C. Sobral Sep 26 '11 at 13:15
  • No, the idea was to write a separate program that the batch script uses to launch process B. – Harry Johnston Sep 26 '11 at 20:23

4 Answers4

11

AFAIK there isn't. Windows lacks execv() which is how bash exec does it. call works for calling batch files (easy, just interpret the file in the current interpreter, similar to bash's . command) but not for exes.

This makes it impossible to write one-liner wrapper scripts for scripts in any language on Windows. You'll always get that "Terminate batch job?" crap on Ctrl+C and killing the batch process (not with Ctrl+C, from task manager say) won't kill the child process. I'm now looking for a C template file to do this wrapping.

UPDATE: Windows does have _execv() in its POSIX compatibility layer in MSVCRT, but AFAIK (haven't tested it) it's just a wrapper around CreateProcess so it will always create a new process, it cannot replace the current process.

capr
  • 231
  • 2
  • 6
  • You can bypass the "Terminate batch job?" as follows: Code your wrapper file as follows, if, say, your called program is a Perl program: ::Why is this box not accepting line breaks? start perl \\wherever\perl_pgms\my_pgm.pl exit The use of "start" instead of "call" will cause the Perl program to start running immediately and asynchronously. Meanwhile the cmd file continues on to its own next instruction, which is an "exit", causing the cmd file program to disappear, and any Ctrl+C will then go directly to the Perl program with no "Terminate batch job?" – fireblood Apr 15 '22 at 11:25
2

I think call does what you want, but I'm not 100% sure.

Paweł Brodacki
  • 6,511
  • 20
  • 23
0

You can construct a dynamic batch command and execute it based on the output of previous operations. I'm not sure this would be a solution in your case, but the following code works just fine.

SET yourDynamicCommand=SET var=1
%yourDynamicCommand%
ECHO var's value is %var%
Abdo
  • 9
  • 1
-1
  • Execute batch-files: call
  • Execute exe-files: No command needed, call it directly by /path/to/exe/something.exe
leonheess
  • 144
  • 3
  • 12