2015-07-21 18 views
0

我正在执行一个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#中执行进程的最佳方式是什么?

回答

0

我得到一个错误,说/ path/to/my/command不能执行 二进制文件。

在这种情况下,我会改变CreateNoWindow为真,不重定向IO,看你做了什么在创建壳,以调试......

就创建了一个背景过程中,一种方法是创建守护进程,并有一个方法来读取/写入信息(网络端口,文件日志等),但只是像开始一个后台任务尝试工作正常。

这是一个使用代码作为起点的快速入侵/示例。而不是使用进程,我只是系统执行命令,在这种情况下找到一个。当在后台运行时,我运行一个Process来计算1秒钟后日志文件中的行数,并在退出时提示您终止该后台查找过程...

using System; 
using System.Runtime.InteropServices; 
using System.Diagnostics; 
using System.IO; 
using System.Threading; 

    namespace nohub.process.exec 
    { 
     class MainClass 
     { 

      [DllImport ("libc")] 
      private static extern int system (string exec); 

      private static string logfile = Path.GetTempFileName(); 

      static void Execute (string command, string commandPath, string arguments, bool nohup = false) 
      { 
       if (!nohup) { 
        var startInfo = new ProcessStartInfo() { 
         FileName = Path.Combine (commandPath, command), 
         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} 
         process.WaitForExit(); 
         using (var output = process.StandardOutput) { 
          Console.Write ("Results: {0}", output.ReadLine()); 
         } 
        } 
       } else { 
        system ("nohup " + Path.Combine (commandPath, command) + " " + arguments + " 2>&1 > " + logfile + " & "); 
       } 

      } 

      public static void Main (string[] args) 
      { 
       Console.WriteLine ("Hello Stack Overflow"); 
       Console.WriteLine ("Temp file: {0}", logfile); 
       Execute ("find", "", "/", true); 
       Thread.Sleep (1000); 
       Execute ("wc", "", "-l " + logfile, false); 
       Console.Write ("\nLet find continue? [y/n]"); 
       if ((Console.ReadKey (false)).KeyChar == 'y') 
        system ("killall find"); 
      } 
     } 
    } 
相关问题