我正在执行一个Linux框中的Mono(C#)进程,但我需要它在我的.exe退出后继续运行。我试图用nohup来做到这一点,但到目前为止还没有取得任何成功。这是现在我的代码:如何用nohup在Mono中执行进程?
void Execute(string command, string commandPath, string arguments, bool nohup = false)
{
ProcessStartInfo startInfo = new ProcessStartInfo()
{
FileName = nohup ? "sh" : Path.Combine(commandPath, script),
Arguments = nohup ? "nohup " + Path.Combine(commandPath, script) + " " + arguments + " &" : arguments,
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true,
UserName = System.Environment.UserName
};
using (Process process = Process.Start(startInfo))
{ // Monitor for exit}
}
所以我检查nohup的参数,如果这是真的我执行shell和发送完整的命令(例如:nohup /path/to/my/command <args> &
)。我收到一个错误,说/path/to/my/command cannot execute binary file
。
我想过的另一件事是打开sh进程,然后发送完整nohup命令到标准输入,但要做到这一点,我不得不重构很多代码,所以如果有一种方法没有这个做我想做的事情,我会走那条路。
那么...在Linux环境(Mono)中,在父进程死后使用nohup保持活跃状态,在C#中执行进程的最佳方式是什么?