【发布时间】:2020-10-05 09:55:48
【问题描述】:
我正在使用 ProcessStartInfo 从 .net 执行一个 linux shell 脚本(“.sh”文件)。我可以将脚本内容复制到 Windows 环境中的文本文件中,但对于 Linux,这不起作用。不是写进程信息,而是写文件信息。
protected async Task StartProcess()
{
try
{
//string txtFileName = TextFilePath + @"\" + "Info.txt";
//File.CreateText(txtFileName);
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
SHFilePath = SHFilePath + @" &>> " + TextFilePath;
FileInfo fileInfo = new FileInfo(SHFilePath);
ProcessStartInfo startInfo = new ProcessStartInfo
{
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
FileName = "/bin/bash",
WindowStyle = ProcessWindowStyle.Hidden,
Arguments = fileInfo.FullName,
WorkingDirectory = WorkingDirectoryPath,
};
using Process process = Process.Start(startInfo);
process.WaitForExit();
process.Dispose();
}
else
{
string path = @"E:\Backup\Segregation\BatchFilesTest";
string batFileName1 = path + @"\" + Guid.NewGuid() + ".bat";
string batFileName2 = "";
ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd.exe", "/c " + batFileName2);
processStartInfo.UseShellExecute = false;
processStartInfo.CreateNoWindow = true;
processStartInfo.WindowStyle = ProcessWindowStyle.Normal;
processStartInfo.WorkingDirectory = @"C:\Program Files (x86)\PBBI CCM\Vault\server";
Process p = new Process();
p.StartInfo = processStartInfo;
p.Start();
p.WaitForExit();
}
await Task.Delay(10);
}
catch(Exception ex)
{
Log.Error(ex, "Error");
}
}
【问题讨论】:
-
“不是写进程信息而是写文件信息”是什么意思?
-
@Efrain 我的意思是我正在调用 .sh 文件。在 .sh 文件中包含命令。该命令将执行另一个进程。程序需要将该输出复制到文本文件,而不是 .sh 文件命令。
-
您是否尝试在 ProcessStartInfo Arguments 属性中仅进一步添加 TextFilePath?
Arguments = fileInfo.FullName + $">> {TextFilePath}"... 将其传递给 FileInfo(..) 构造函数可能会丢弃“>>”部分(可能使用调试器在运行时检查 fileInfo.FullName)