StandardOutput.ReadToEnd Hangs Indefinitely

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 = "";


            logCapture = process.StandardOutput.ReadToEnd();

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

        return new psResult
        { HasErrors = false, ErrorMessage = $"{scriptToExecute}.", RawResult = logCapture };


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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!

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x