Why does the cmd.exe / C command not end when called through Process.Start ()?

I am trying to run a command through the command line from an ASP.Net web application. I see that the process starts in the task manager on the web server, however the process just sits there and never exits and does not execute the commands that 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 tried both with and without reading standard output.

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

+7
source share
3 answers

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

For example, I saw in your comments where you tried "echo test> C: \ test.txt". In Windows Server 2008 (and Windows 7), the root directory requires administrator rights to create files. If this runs on IIS, I assume that your IIS user is not an administrator, and you get security exceptions.

In addition, some teams may require increased privilege due to UAC. I don’t remember exactly, but I assume that if these commands are caught by the UAC, then the process is waiting for the confirmation of the UAC ... Which, I believe, you cannot provide through the command line.

This problem will not be visible if you log in and execute it directly ... if you do not log in with a workflow user account.

So, the very first thing you need to do is find out what exactly you are trying to run, and see if the user performing the workflow can perform these actions. Security protects you, so be careful when giving the user additional rights.

The reason that it can work on one machine compared to another depends on the OS on which these machines work and on the configuration of the user executed by the commands.

If this is really a security issue, as I suspect, then you should send a question to serverfault.com to find out what permission settings you need to run various commands under the workflow user.

You can look at the event logs in the machines to see if there were any warnings or errors that occurred in the command. Sometimes such things may appear there to give you a little more information about what happened.

+2
source

After the CMD is transferred, the control is passed to the shell. Better add 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(); } } } 
+1
source

I call cmd.exe to start the node module on Windows. Obviously, npm must be installed first with the node module that I need, and then I can call the module with args in C #. The problem was that cmd.exe did not disconnect, I would have to use Task Mgr (just like this question!).

  //This requires npm, and the module installed by npm... ProcessStartInfo psi = new ProcessStartInfo(); psi.FileName = "cmd.exe"; psi.WindowStyle = ProcessWindowStyle.Hidden; psi.UseShellExecute = true; //closes command window psi.Arguments = "/c <nodemodulename>" + file1 + ".txt " + file2 + ".log"; Process p = Process.Start(psi); p.Close(); 

The /c argument was the key to closing cmd.exe. When I started, I included the /K argument, which holds damn. Solvable. Hope this helps. (This is similar to the ancients, but we always forget)

0
source

All Articles