为什么“cmd.exe /C 命令”不显示通过 Process.Start() 调用时结束?
我正在尝试通过命令提示符从 ASP.Net Web 应用程序运行命令。我可以看到该进程在 Web 服务器上的任务管理器中启动,但是该进程只是坐在那里,永远不会退出,也不会运行我指定的命令。
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();
我尝试过读取和不读取标准输出。
应用程序将挂在 process.WaitForExit(); 上,直到我通过任务管理器终止该进程。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为我们需要了解您实际上正在尝试处理哪些命令,以便确定发生了什么。我们还需要知道服务器正在运行什么操作系统。
例如,我确实在您的评论中看到您在 Windows Server 2008(和 Windows 7)下尝试了“echo test > C:\test.txt”,根目录需要管理员权限才能创建文件。如果这是在 IIS 下执行,我的猜测是您的 IIS 用户不是管理员,并且您会遇到安全异常。
此外,由于 UAC,许多命令可能需要提升权限。我记得不太清楚,但我猜测如果这些命令被 UAC 捕获,那么该进程正在等待 UAC 确认...我相信您无法通过命令行提供这一点。
如果您登录机器并直接执行,则不会出现此类问题...除非您使用工作进程用户帐户登录。
因此,您需要做的第一件事就是弄清楚您正在尝试运行什么,并查看正在执行工作进程的用户是否可以执行这些操作。安全性是为了保护您,因此在向用户授予额外权限时要小心。
它在一台机器上与另一台机器上工作的原因再次取决于这些机器正在运行的操作系统以及执行命令的用户的配置。
如果这确实是一个安全问题,正如我怀疑的那样,那么您应该在 serverfault.com 上发布一个问题,询问在工作进程用户下执行各种命令需要哪些权限集。
您可以查看计算机事件日志以查看是否有关于该命令的任何警告或错误。有时,类似的事情会出现在那里,为您提供有关发生的情况的更多信息。
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.
一旦传递给 CMD,控制权就传递给了 shell。最好像这样添加一个关闭:
Once passed to CMD, the control has passed to the shell. It's better to add a close it like this:
我正在调用 cmd.exe 在 Windows 中启动节点模块。显然必须首先安装 npm 以及我需要的节点模块,然后我可以在 C# 中使用 args 调用该模块。问题是,cmd.exe 不会关闭,我必须使用 Task Mgr (就像这个问题一样!)。
/c
参数是关闭 cmd.exe 的关键。当我开始时,我输入了/K
参数,这让该死的东西保持运行。解决了。希望这有帮助。 (这就像古代一样,但我们总是忘记)I am calling cmd.exe to start a node module in Windows. npm obviously must be installed first, along with the node module I need, and then I can call the module with args in C#. Problem was, the cmd.exe would not shut off, I'd have to use Task Mgr (just like this question!).
The
/c
argument was key to closing down cmd.exe. When I started out, I had put in the/K
argument, which keeps the darned thing running. Solved. Hope this helps. (This is like ancient, but we always forget)