简体   繁体   English

C#进程是否退出批处理文件状态?

[英]C# Process exit on batch file status?

static void Main(string[] args) 
{
    Process process=Process.Start(@"C:\Users\dalvi\Desktop\iisresetdaily.bat");

    if(process.ExitCode==0)
        SendMail("Sucesss in iisreset..", "bye"); // --> case 1
    else
        SendMail("Failed iisreset..", "bye"); // --> case 2
}

Edit : My SendMail method is working fine. 编辑:我的SendMail方法工作正常。

I've written iisresetdaily.bat file which is doing iisreset /stop and iisreset /start . 我已经编写了iisresetdaily.bat文件,该文件正在执行iisreset /stopiisreset /start When it executes successfully, I need to send success in iisrest mail --> case 1 , otherwise case 2 . 成功执行后,我需要在iisrest邮件中发送成功--> case 1 ,否则发送case 2

But when I edit the .bat file and put some random words so batch file is failing, but still it's sending case 1 mail meaning success in iisreset . 但是,当我编辑.bat文件并放一些随机单词时,批处理文件就失败了,但是仍然发送case 1邮件,表示iisreset成功。

Am I doing wrong to check status of batch file using Process.ExitCode here? 我在这里使用Process.ExitCode检查批处理文件的状态做错了吗?

If batch file failed, I need to send case 2 failed iisreset . 如果批处理文件失败,则需要发送case 2失败的iisreset

That is because your "batch file" may not be able to reset IIS or whatever buts it does complete execution properly and exit with success [ie statusCode=0]. 那是因为您的“批处理文件”可能无法重置IIS或其他任何东西,但是它可以正确完成执行并成功退出[ie statusCode = 0]。

Try to make your batch like: 尝试使您的批次像:

@echo Started
--Some code that works
--Some code that surely fails [But not break the batch, as in calling external program]
@echo Finished

You will notice that though "Some code that surely fails", you still witness "Finish". 您会注意到,尽管“某些代码肯定会失败”,但您仍然看到“完成”。

I believe the problem is that the .bat script is not providing any useful error code / exit code for you to check. 我相信问题是.bat脚本没有提供任何有用的错误代码/退出代码供您检查。 This question might give you some useful information about how to set it up correctly. 这个问题可能会给您一些有关如何正确设置的有用信息。

Also, see this question for info on terminating a batch when encountering an error. 另外, 有关遇到错误时终止批处理的信息 ,请参阅此问题

You would need to wait the process for exit before you can use ExitCode : 您可能需要等待退出过程才能使用ExitCode

using(var process=Process.Start(@"iisresetdaily.bat")) {
    process.WaitForExit();

    var message=
        0!=process.ExitCode
            ?"Failed iisreset .."
            :"Sucesss in iisreset ..";

    SendMail(message, "bye"); 
}

It would be better if you can provide the content of the batch file, that we can help to figure out why. 如果您可以提供批处理文件的内容,那将更好,因为我们可以帮助您找出原因。

Are you checking iisreset exit codes in the batch script and propagating them? 您是否正在检查批处理脚本中的iisreset退出代码并进行传播?

According to http://support.microsoft.com/kb/202013 they have an example showing that it will return an error code of 1 if it fails. 根据http://support.microsoft.com/kb/202013,他们有一个示例显示,如果失败,它将返回错误代码1。

@echo off
IISRESET /STOP /NOFORCE
if errorlevel == 1 goto EXIT
copy %systemroot%\system32\LogFiles\W3SVC1 d:\backup\W3SVC1
IISRESET /START
:EXIT

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM