在Windows bat文件中运行另一个程序而不创建子进程

zho*_*shu 17 svn windows batch-file post-commit svn-hooks

我有一个带有post-commit钩子的subversion服务器来做某事.

我希望尽快签入,而不是等待钩子脚本.但是按照设计,Subversion post-commit钩子脚本将一直运行,直到所有进程退出,所以使用如下的东西:

启动another_prog ...

在钩子bat文件中没有用.

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

Cor*_*ger 18

同步.第二个记事本在你关闭第一个记事本之前不会启动.

notepad.exe c:\temp\a.txt
notepad.exe c:\temp\b.txt
Run Code Online (Sandbox Code Playgroud)

异步:即使你没有关闭第一个记事本,第二个记事本也会启动.

start notepad.exe c:\temp\a.txt
start notepad.exe c:\temp\b.txt
Run Code Online (Sandbox Code Playgroud)

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

编辑:以下评论是由原始海报@zhongshu在其他地方做出的.我只是在这里复制它:

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

假设他知道他在说什么,我错了......不值得.


zho*_*shu 12

我找到了一个方法来解决我的问题,编译以下c代码并将exe输出命名为runjob.exe,然后在hook 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; 
} 
Run Code Online (Sandbox Code Playgroud)

  • 以下是源代码的已编译可执行文件的链接:https://mega.nz/#!u8tD0YAY!OuULXWhzG4VThwLAbkjpRdTfg6XLgxFmmpNP2hnzb1w (3认同)

jas*_*onh 7

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

schtasks /run /tn NameOfYourTaskHere
Run Code Online (Sandbox Code Playgroud)

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


OJ.*_*OJ. 6

用:

start cmd /c "your command"
Run Code Online (Sandbox Code Playgroud)

干杯。

  • 这不适用于 Windows 上的 SVN 提交后挂钩。它仍然会等待子进程完成。参见 [Corey Trager 的回答](http://stackoverflow.com/questions/1536205/running-another-program-in-windows-bat-file-and-not-create-child-process/1569941#1569941) (2认同)