我有一个Subversion服务器,其中有一个后提交挂钩(post-commit hook)用于执行某些操作。
我希望检入过程快速完成,而不是等待挂钩脚本运行。但按照设计,Subversion后提交挂钩脚本将一直运行,直到所有子进程都退出,因此在挂钩批处理文件中使用类似以下命令:
start another_prog...
没有任何用处。
因此,我想知道如何在Windows批处理文件中运行另一个程序,而不创建子进程或使子进程与父进程分离。
我有一个Subversion服务器,其中有一个后提交挂钩(post-commit hook)用于执行某些操作。
我希望检入过程快速完成,而不是等待挂钩脚本运行。但按照设计,Subversion后提交挂钩脚本将一直运行,直到所有子进程都退出,因此在挂钩批处理文件中使用类似以下命令:
start another_prog...
没有任何用处。
因此,我想知道如何在Windows批处理文件中运行另一个程序,而不创建子进程或使子进程与父进程分离。
同步。只有在关闭第一个记事本之后,第二个记事本才会启动。
notepad.exe c:\temp\a.txt
notepad.exe c:\temp\b.txt
异步:即使您没有关闭第一个记事本,第二个记事本也会启动。
start notepad.exe c:\temp\a.txt
start notepad.exe c:\temp\b.txt
关于start命令的更多信息:
http://www.robvanderwoude.com/ntstart.php
编辑:原帖作者@zhongshu在其他地方发表了以下评论,我只是将其复制到这里:
使用“start cmd /c”无效,因为SVN post-commit钩子将等待钩子和由钩子创建的子进程退出。这是SVN的设计。 我找到了一个解决方案,请参考:http://svn.haxx.se/users/archive-2008-11/0301.shtml
假设他知道自己在说什么,那么我错了,并且不配得到。
我找到了一种解决我的问题的方法,编译以下C代码并将可执行文件命名为runjob.exe,然后在hook批处理文件中使用“runjob another_prog”,现在可以正常运行。
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
int _tmain()
{
char * pCmd = ::GetCommandLine();
// skip the executable
if (*pCmd++ == L'"')
{
while (*pCmd++ != L'"');
}
else
{
while (*pCmd != NULL && *pCmd != L' ')
++pCmd;
}
while (*pCmd == L' ')
pCmd++;
STARTUPINFO si;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
PROCESS_INFORMATION pi;
ZeroMemory( &pi, sizeof(pi) );
// Start the child process.
BOOL result = CreateProcess
(
NULL, // No module name (use command line)
pCmd, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set bInheritHandles to FALSE
DETACHED_PROCESS, // Detach process
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi // Pointer to PROCESS_INFORMATION structure (returned)
);
if (result) return 0;
char msg[2048];
FormatMessage
(
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
::GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_SYS_DEFAULT),
msg, sizeof(msg),
NULL
);
fputs(msg, stderr);
_flushall();
return -1;
}
another_prog
提供命令行参数的一些规定怎么样?否则,我就得编写另一个批处理脚本来调用带有参数的exe文件。 - evandrix您可以创建一个定时任务,执行批处理脚本或其他长时间运行的可执行文件。将其设置为在过去运行一次,并不设置在没有更多运行计划时删除该任务。然后,在您的Subversion钩子脚本中,添加以下行:
schtasks /run /tn NameOfYourTaskHere
我进行了一项测试以确认,即通过让我的计划任务运行Notepad++,Notepad++可执行文件显示为svchost.exe的子进程,而不是我从schtasks
命令执行的cmd.exe窗口。
schtasks /run /tn NameOfTask
是最佳解决方案。 - truemedia使用:
start cmd /c "your command"
尝试使用 cmd /c "你的命令"
您可以创建一个混合批处理/JScript文件(即能够运行嵌入式JScript的批处理文件),其中JScript部分将使用shell.Run(prog, 0, false)以分离模式运行another_prog:
@if (@X)==(@Y) @end /* JScript comment
rem put any batch code that runs before another_prog here
@cscript //E:JScript //nologo "%~f0" "another_prog" 0 false
rem put any batch code that runs after another_prog here
exit /b %errorlevel%
@if (@X)==(@Y) @end JScript comment */
var ARGS = WScript.Arguments;
var shell = new ActiveXObject("WScript.Shell");
shell.Run(ARGS.Item(0),ARGS.Item(1),ARGS.Item(2));`
start cmd /c startWebLogic.cmd
不起作用? - Cheeso