Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why won't "cmd.exe /C command" end when called via Process.Start()?

Tags:

c#

process

cmd

I'm trying to run a command via command prompt from an ASP.Net web application. I can see the process start in task manager on the web server, however the process just sits there and never exits nor does it run the commands I specified.

            Process process = new Process();
            ProcessStartInfo startInfo = new ProcessStartInfo();

            startInfo.FileName = "cmd.exe";
            startInfo.Arguments = "/C " +command;
            startInfo.UserName = "myuser";
            startInfo.UseShellExecute = false;
            startInfo.RedirectStandardOutput = true;
            startInfo.Domain = "mydomain";
            startInfo.CreateNoWindow = true;

            String pass = "mypass";
            System.Security.SecureString secPass = new System.Security.SecureString();

            foreach (char c in pass.ToCharArray())
            {
                secPass.AppendChar(c);
            }
            secPass.MakeReadOnly();

            startInfo.Password = secPass;

            process.StartInfo = startInfo;
            process.Start();
            //output = process.StandardOutput.ReadToEnd();
            process.WaitForExit();
            process.Close();

I've tried both with and without reading the standard output.

The application will hang on process.WaitForExit(); until I kill the process via task manager.

like image 888
Petey B Avatar asked Jan 28 '11 16:01

Petey B


2 Answers

I think we need to understand what commands you are actually trying to process in order to determine what's going on. Also we need to know what OS the server is running.

For example, I did see in your comments where you tried "echo test > C:\test.txt" Under Windows Server 2008 (and Windows 7) the root directory requires administrator permissions in order to create files. If this is executing under IIS, my guess is that your IIS user isn't an administrator and you are getting security exceptions.

Also, a number of commands may require elevated priviledges due to UAC. I don't remember exactly, but I'm guessing that if those commands are being caught by UAC then the process is waiting for UAC confirmation... Which I believe you cannot supply via a command line.

This type of problem won't be seen if you log into the machine and execute it directly... unless you are logging in with the worker process user account.

So, the very first thing you need to do is figure out what it is you are trying to run and see if the user the worker process is executing under can even perform those actions. Security is there to protect you, so be careful about granting additional permissions to the user.

The reason why it might work on one machine versus another again depends on the OS's those machines are running and the configuration of the user the commands are executing under.

If this is truly a security issue, as I suspect, then you should post a question on serverfault.com to ask what permission sets you need to execute various commands under your worker process user.

You might look at the machines event logs to see if there were any warnings or errors thrown about the command. Sometimes things like this can show up there to give you a bit more information as to what happened.

like image 120
NotMe Avatar answered Oct 09 '22 21:10

NotMe


Once passed to CMD, the control has passed to the shell. It's better to add a close it like this:

private void closeSubProcess()
    {
        Process[] currentProcesses = Process.GetProcesses();
        foreach (Process p in currentProcesses)
        {
            string s = p.ProcessName;
            s = s.ToLower();
            if (s.CompareTo("YOURPROGRAMNAMEHERE") == 0)
            {
                p.CloseMainWindow();
                p.Close();
            }
        }
    }
like image 39
jschorr Avatar answered Oct 09 '22 21:10

jschorr