Running another program in Windows bat file and not create child process

前端 未结 7 574
悲哀的现实
悲哀的现实 2020-12-01 12:46

I have subversion server with a post-commit hook to do something.

I want the checkin finish soon, not wait the hook script. But by design, the Subversion post-commi

相关标签:
7条回答
  • 2020-12-01 13:11

    What you can do is create a Scheduled Task that executes the batch script or other executable that runs for a long time. Set it to run once, in the past and don't set it to delete the task when no more runs are scheduled. Then in your Subversion hook script, put the following line in:

    schtasks /run /tn NameOfYourTaskHere
    

    I confirmed with a test by having my scheduled task run Notepad++ and the Notepad++ executable showed up as a child of svchost.exe, not the cmd.exe window that I executed the schtasks command from.

    0 讨论(0)
  • Synchronous. The second notepad won't launch until you close the first.

    notepad.exe c:\temp\a.txt
    notepad.exe c:\temp\b.txt
    

    Asynchronous: The second notepad will launch even if you haven't closed the first.

    start notepad.exe c:\temp\a.txt
    start notepad.exe c:\temp\b.txt
    

    More info about the start command:
    http://www.robvanderwoude.com/ntstart.php

    EDIT: The following comment was made elsewhere by @zhongshu, the original poster. I'm only copying it here:

    start cmd /c doesn't work because SVN post-commit hook will wait for the hook and the child process created by the hook exit. It's the design of SVN. I have found a solution, Please refer: http://svn.haxx.se/users/archive-2008-11/0301.shtml

    Assuming that he knows what he's talking about, I'm wrong and...undeserving.

    0 讨论(0)
  • 2020-12-01 13:15

    Try cmd /c "your command"

    0 讨论(0)
  • 2020-12-01 13:17

    I found a method to resolve my question, compile the following c code and named the exe output as runjob.exe, and then in the hook bat file, use " runjob another_prog " , now it's ok.

    #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; 
    } 
    
    0 讨论(0)
  • 2020-12-01 13:19

    You can create a hybrid batch/JScript file (i.e. a batch file able to run embedded JScript) where the JScript part will run another_prog in detached mode with shell.Run(prog, 0, false):

    @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));`
    
    0 讨论(0)
  • 2020-12-01 13:31

    Use:

    start cmd /c "your command"
    

    Cheers.

    0 讨论(0)
提交回复
热议问题