如何从另一个 bat 文件在后台运行 bat 文件?

我有一个“设置”脚本,我运行在早上,它启动所有我需要的程序。现在其中一些需要额外的环境设置,所以我需要用小的 BAT 脚本包装它们。

如何在后台运行 WindowsXP 上的这样一个脚本?

CALL env-script.bat同步运行它,也就是说,安装脚本只能在 env-script 中的命令终止后才能继续。

START/B env-script.bat在同一个命令提示符下运行另一个 CMDexe 实例,使其处于一种非常混乱的状态(我看到嵌套 CD.exe 的输出,键盘死机了一段时间,脚本没有执行)。

START/B CMD env-script.bat产生相同的结果。在 CMD 的旗帜似乎没有匹配我的法案。

261347 次浏览

Since START is the only way to execute something in the background from a CMD script, I would recommend you keep using it. Instead of the /B modifier, try /MIN so the newly created window won't bother you. Also, you can set the priority to something lower with /LOW or /BELOWNORMAL, which should improve your system responsiveness.

Create a new C# Windows application and call this method from main:

public static void RunBatchFile(string filename)
{
Process process = new Process();


process.StartInfo.FileName = filename;


// suppress output (command window still gets created)
process.StartInfo.Arguments = "> NULL";


process.Start();
process.WaitForExit();
}

Actually, the following works fine for me and creates new windows:

test.cmd:

@echo off
start test2.cmd
start test3.cmd
echo Foo
pause

test2.cmd

@echo off
echo Test 2
pause
exit

test3.cmd

@echo off
echo Test 3
pause
exit

Combine that with parameters to start, such as /min, as Moshe pointed out if you don't want the new windows to spawn in front of you.

This works on my Windows XP Home installation, the Unix way:

call notepad.exe &

Two years old, but for completeness...

Standard, inline approach: (i.e. behaviour you'd get when using & in Linux)

START /B CMD /C CALL "foo.bat" [args [...]]

Notes: 1. CALL is paired with the .bat file because that where it usually goes.. (i.e. This is just an extension to the CMD /C CALL "foo.bat" form to make it asynchronous. Usually, it's required to correctly get exit codes, but that's a non-issue here.); 2. Double quotes around the .bat file is only needed if the name contains spaces. (The name could be a path in which case there's more likelihood of that.).

If you don't want the output:

START /B CMD /C CALL "foo.bat" [args [...]] >NUL 2>&1

If you want the bat to be run on an independent console: (i.e. another window)

START CMD /C CALL "foo.bat" [args [...]]

If you want the other window to hang around afterwards:

START CMD /K CALL "foo.bat" [args [...]]

Note: This is actually poor form unless you have users that specifically want to use the opened window as a normal console. If you just want the window to stick around in order to see the output, it's better off putting a PAUSE at the end of the bat file. Or even yet, add ^& PAUSE after the command line:

START CMD /C CALL "foo.bat" [args [...]] ^& PAUSE

Other than foreground/background term. Another way to hide running window is via vbscript, if is is still available in your system.

DIM objShell
set objShell=wscript.createObject("wscript.shell")
iReturn=objShell.Run("yourcommand.exe", 0, TRUE)

name it as sth.vbs and call it from bat, put in sheduled task, etc. PersonallyI'll disable vbs with no haste at any Windows system I manage :)

Actually is quite easy with this option at the end:

c:\start BATCH.bat -WindowStyle Hidden