c# - 无法使用 C# 中的 Process 类获取有关在 bash 中运行的命令的信息
问题描述
我在 Linux 上的 azure app 服务中部署了 ASP.NET 核心 MVC 应用程序。当我尝试运行 /bin/bash 并执行一些脚本时,例如:dotnet run test.dll 或 javac Test.java,当我尝试获取有关已退出进程的一些信息(如 MemotyUsed 和 TimeUsed)时,我收到以下错误:“无法检索有关进程或线程的指定信息。它可能已退出或可能具有特权“
当我在 Windows 上运行该应用程序时,一切正常。我还尝试在 linux os 上的 docker 中运行该应用程序,因为我认为问题来自 azure app 服务。但是,在 docker 容器中再次引发了相同的错误。所以我认为这个问题来自linux os。
ProcessExecutionResult processExecutionResult = new ProcessExecutionResult();
using (Process process = new Process())
{
process.StartInfo.FileName = "/bin/bash";
process.StartInfo.Arguments = arguments;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.Start();
if (!string.IsNullOrEmpty(input))
{
await process.StandardInput.WriteLineAsync(input);
await process.StandardInput.FlushAsync();
process.StandardInput.Close();
}
bool exited = process.WaitForExit(ProcessMaxRunningTime);
if (!exited)
{
process.Kill();
processExecutionResult.Type = ProcessExecutionResultType.TimeLimit;
}
string output = await process.StandardOutput.ReadToEndAsync();
string errors = await process.StandardError.ReadToEndAsync();
processExecutionResult.ErrorOutput = errors;
processExecutionResult.ReceivedOutput = output;
processExecutionResult.ExitCode = process.ExitCode;
processExecutionResult.TimeWorked = process.ExitTime - process.StartTime;
processExecutionResult.PrivilegedProcessorTime = process.PrivilegedProcessorTime;
processExecutionResult.UserProcessorTime = process.UserProcessorTime;
}
当我尝试访问process.ExitTime
,process.StartTime
或有关进程的一些其他信息时出现错误:无法检索有关进程或线程的指定信息。它可能已经退出或可能被特权抛出。
解决方案
推荐阅读
- python - 在 Python Pandas 中读取文本文件有问题?
- r - 在保存的数据和模型(.RData)上使用预测时导致 R 错误“没有适用的方法”的原因是什么?
- c++ - 模仿 OpenGL 缓冲区的 shared_ptr 的行为
- java - 声明其他 `TYPE` 目标是否与 `TYPE_USE` 多余?
- javascript - ReactJS 没有渲染我的文本(我是一个刚开始学习的菜鸟)
- python - 使用 Pandas 进行数据分析没有任何结果
- javascript - 删除时无法更新数组状态 [React.js]
- android - MainActivity.kt 文件中无法识别按钮 ID Kotlin
- mysql - 高效的 SQL 查询以查找连续数字数据中的差距 (MySQL)
- javascript - node:internal/modules/cjs/loader:922 抛出错误;^ 错误:找不到模块