19

我有一个带有提交后挂钩的颠覆服务器来做某事。

我希望签入尽快完成,而不是等待挂钩脚本。但是按照设计,Subversion post-commit 钩子脚本将一直运行,直到所有进程退出,所以使用类似:

开始另一个程序...

在钩子bat文件中没有用。

所以我想知道如何在 Windows bat 文件中运行另一个程序,它不会创建子进程或让子进程与父进程分离。

4

7 回答 7

22

同步。在您关闭第一个记事本之前,第二个记事本不会启动。

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

有关启动命令的更多信息:
http ://www.robvanderwoude.com/ntstart.php

编辑:以下评论是由原发帖者@zhongshu 在别处发表的。我只是在这里复制它:

start cmd /c 不起作用,因为 SVN post-commit 钩子将等待钩子和钩子退出创建的子进程。这是SVN的设计。我找到了解决方案,请参考: http ://svn.haxx.se/users/archive-2008-11/0301.shtml

假设他知道他在说什么,我错了,而且……不值得。

于 2009-10-15T02:00:58.803 回答
14

我找到了解决问题的方法,编译以下c代码并将exe输出命名为runjob.exe,然后在钩子bat文件中,使用“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; 
} 
于 2009-10-17T13:20:44.570 回答
8

您可以做的是创建一个计划任务来执行批处理脚本或其他长时间运行的可执行文件。将其设置为过去运行一次,并且不要将其设置为在不再安排运行时删除任务。然后在您的 Subversion 挂钩脚本中,输入以下行:

schtasks /run /tn NameOfYourTaskHere

我通过让我的计划任务运行 Notepad++ 来确认测试,并且 Notepad++ 可执行文件显示为 svchost.exe 的子项,而不是我执行schtasks命令的 cmd.exe 窗口。

于 2009-10-15T21:49:33.137 回答
6

采用:

start cmd /c "your command"

干杯。

于 2009-10-12T02:18:05.137 回答
1

尝试cmd /c "your command"

于 2009-10-08T07:43:19.987 回答
1

您可以使用 Windows 任务调度程序命令行界面“schtasks /run”来启动运行“another_prog”的作业吗?您必须提前创建工作。曾经还有一个带有 Windows (NT) 资源工具包的“SOON”程序,它可以为“AT”命令调度程序创建动态条目,以便在几分钟内运行不需要提前设置作业的作业,稍加搜索仍然可以找到它。

于 2009-10-12T01:59:04.610 回答
0

您可以创建一个混合批处理/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));`
于 2017-03-08T20:27:21.433 回答