0

First, I've tried several solutions including the ones listed here. ProcessStartInfo hanging on "WaitForExit"? Why? and StandardOutput.ReadToEnd() hangs

None of them are working for me and all of them timeout or hang. Do you see anything obvious in my code that I'm doing wrong? Basically, I am trying to run a shell script via C#. The script runs fine, and takes a while, when you run it manually. (Like one minute) In my code it is HANGING on ReadToEnd. Here's the code...

        public psResult RunScript(string scriptToExecute, string scriptArgument)
    {
        var logEnabled = true;

        scriptToExecute = ConfigurationManager.AppSettings["s-path"] + scriptToExecute;

        if (!System.IO.File.Exists(scriptToExecute))
            return new psResult
            { HasErrors = true, ErrorMessage = $"{scriptToExecute} is not found!" };

        var process = new Process
        {
            StartInfo =
            {
                UseShellExecute = false,
                RedirectStandardOutput = true,
                FileName = ConfigurationManager.AppSettings["exe"],
                Arguments = $"{scriptToExecute} {scriptArgument}"
            }
        };

        string logCapture = "";

        try
        {
            process.Start();

            logCapture = process.StandardOutput.ReadToEnd();

            process.WaitForExit();
        }
        catch (Exception e)
        {
            return new psResult
            { HasErrors = true, ErrorMessage = $"{e.InnerException}", RawResult = logCapture };
        }

        return new psResult
        { HasErrors = false, ErrorMessage = $"{scriptToExecute}.", RawResult = logCapture };
    }
WebDevGuy2
  • 1,159
  • 1
  • 19
  • 40

1 Answers1

0

It turned out to be a script permission issue for my machine. Once I changed it to ByPass it worked. So, it wasn't the code!

WebDevGuy2
  • 1,159
  • 1
  • 19
  • 40