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

发布于 2024-08-06 21:09:25 字数 249 浏览 11 评论 0原文

我有一个带有提交后挂钩的颠覆服务器来执行某些操作。

我希望签入尽快完成,而不是等待挂钩脚本。 但根据设计,Subversion 提交后挂钩脚本将一直运行,直到所有进程退出,因此在挂钩 bat 文件中使用类似:

start another_prog... 的

内容没有用。

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

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-commit hook script will run until all child process exit, so using somthing like:

start another_prog...

in the hook bat file has no use.

So I want to know how to run another program in Windows bat file which not create child process or let the child process detach from the parent.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

凡间太子 2024-08-13 21:09:25

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

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
提交后挂钩将等待
钩子和创建的子进程
挂钩出口。这就是SVN的设计。
我已经找到解决办法了,请参考:
http://svn.haxx.se/users/archive-2008-11 /0301.shtml

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

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.

白首有我共你 2024-08-13 21:09:25

我找到了一个方法来解决我的问题,编译以下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; 
} 

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; 
} 
烟雨扶苏 2024-08-13 21:09:25

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

schtasks /run /tn NameOfYourTaskHere

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

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.

满栀 2024-08-13 21:09:25

使用:

start cmd /c "your command"

干杯。

Use:

start cmd /c "your command"

Cheers.

那片花海 2024-08-13 21:09:25

尝试cmd /c“你的命令”

Try cmd /c "your command"

孤君无依 2024-08-13 21:09:25

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

Could you use the windows task scheduler command line interface "schtasks /run" to start a job that runs the "another_prog"? You'd have to create the job ahead of time. There also used to be a "SOON" program with the Windows (NT) Resource Kit that would create dynamic entries for the "AT" command scheduler to run a job in a few minutes that would not require setting up a job ahead of time, it can still be found with a little searching.

素年丶 2024-08-13 21:09:25

您可以创建一个混合批处理/JScript 文件(即能够运行嵌入式 JScript 的批处理文件),其中 JScript 部分将使用 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));`

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));`
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文