我遇到了在C#中启动的进程无法输出到控制台的问题,尽管它的输出被重定向。
我在unityci docker容器中运行一个控制台应用程序,它允许我以批处理模式启动unity,并将某些内容输出到控制台。
如果我使用bash启动unity,使用unity-editor -projectPath myProject -executeMethod myMethod -logFile -,我会得到控制台中显示的所有输出。
如果我使用C#启动一个带有相同参数的bash进程,则不会得到任何输出。
下面是我用来启动一个新进程的代码:
void StartProcess()
{
string argsString = "-projectPath myPath -executeMethod myMethod -logFile -";
ProcessStartInfo startInfo = new ProcessStartInfo(argsString)
{
FileName = "/bin/bash",
Arguments = $"-c unity-editor \"{argsString}\"",
RedirectStandardOutput = true,
RedirectStandardError = false,
UseShellExecute = false,
CreateNoWindow = true,
};
using Process proc = Process.Start(startInfo);
OutputDataReceived += OnOutputDataReceived;
proc.BeginOutputReadLine();
proc.WaitForExit();
}
void OnOutputDataReceived(object obj, DataReceivedEventArgs args)
{
if(args.Data.StartsWith("[")
Console.WriteLine(args.Data);
}unity-editor命令是我正在使用的unityci docker容器的一部分。它执行以下操作:
#!/bin/bash
if [ -d /usr/bin/unity-editor.d ] ; then
for i in /usr/bin/unity-editor.d/*.sh; do
if [ -r $i ]; then
. $i
fi
done
fi
xvfb-run -ae /dev/stdout "$UNITY_PATH/Editor/Unity" -batchmode "$@"最后,我得到了一个控制台应用程序,它运行一个启动另一个应用程序的可执行文件,控制台输出在两者之间的某个地方丢失了。有人能解释一下输出的去向以及如何在控制台中显示吗?
发布于 2021-11-11 11:24:16
我想通了。问题是我是如何开始这个过程的。而不是启动bash实例来运行脚本,如下所示:
ProcessStartInfo startInfo = new ProcessStartInfo("/bin/bash", $"-c unity-editor \"{argsString}\"");我应该直接将脚本作为一个进程启动:
ProcessStartInfo startInfo = new ProcessStartInfo("unity-editor", argsString);我的印象是,您不能直接将bash脚本作为进程启动,因为那样会抛出System.ComponentModel.Win32Exception: Exec format error异常,但是将#!/bin/bash作为脚本的第一行可以让它们顺利运行。
https://stackoverflow.com/questions/69899810
复制相似问题