如何在C#中执行ubuntu shell命令以使exitCode为0

问题描述 投票:0回答:1

我通过C#代码运行Ubuntu Shell命令,当我在Ubuntu中运行此脚本时,它显示为'语法正常',但是当我通过C#代码运行它时,它总是返回exitCode - 1.如果ID错误,请更正我的代码。

            string _exitCode="";
            Process proc = new Process();
            proc.StartInfo.FileName = "/bin/bash"; 

            proc.StartInfo.Arguments ="-c \" apachectl -t -f /etc/apache2/sites-enabled/example.conf \""; 

            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.RedirectStandardOutput = true;
            proc.StartInfo.RedirectStandardError = true;

            proc.Start();

            proc.BeginOutputReadLine();

            proc.StandardError.ReadToEndAsync();

            proc.WaitForExit(3000);

            if (!proc.HasExited)
                proc.Kill();

            _exitCode = proc.ExitCode.ToString();
c# ubuntu
1个回答
0
投票

纠正命令:

"-c \"/etc/init.d/apachectl -t -f /etc/apache2/sites-enabled/example.conf 2>&1\"";
© www.soinside.com 2019 - 2024. All rights reserved.