【问题标题】:Run bash from .Net Core independent from main process linux c#从独立于主进程linux c#的.Net Core运行bash
【发布时间】:2020-09-07 09:16:46
【问题描述】:

您好,有一项服务需要执行独立于 .Core 进程的更新后的 shell 脚本。我试过 /bin/bash、/bin/nohup 和 /bin/setsid。但是每次脚本停止 systemd 服务时,脚本似乎也被停止了。我怎样才能让它独立?

private static Process StartLinuxUpdateScript(string pathToUpdateScript, string pathToUpdateZip)
    {
        var process = new Process()
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = "/bin/setsid",
                UseShellExecute = true,
                Arguments = $" bash {pathToUpdateScript} {pathToUpdateZip}"
            }
        };
        return process;
    }

【问题讨论】:

    标签: c# linux bash .net-core


    【解决方案1】:

    也许有帮助:

     public void RunProcess(string fileName, string arg)
            {
                var process = new Process
                {
                    StartInfo =
                    {
                        RedirectStandardOutput = true,
                        RedirectStandardError = true,
                        FileName = fileName,
                        Arguments = arg ,
                        UseShellExecute = false,
                        CreateNoWindow = true
                    },
                    EnableRaisingEvents = true
                };
    
                process.Start();
                string processOutput; 
                while ((processOutput = process.StandardError.ReadLine()) != null)
                {
                    Console.WriteLine(processOutput);
                }
    
                process.Dispose();
            }
    

    【讨论】:

    • 谢谢...但我不想等待这个过程。启动的进程将停止我的 systemd 服务,这意味着我无法重定向输出或执行读取行。我刚刚通过覆盖文件并重新启动 systemd 服务解决了更新过程,因为我可以在服务运行期间覆盖文件。但仍然想知道是否可以独立于主进程执行 shell 脚本
    • 您可以创建新线程并在其上运行 shell。我希望你能看到你的程序中的任何错误。如果您希望 shell 运行到其他线程,请使用 fire 并忘记扩展功能
    猜你喜欢
    • 2016-06-19
    • 1970-01-01
    • 2011-06-22
    • 1970-01-01
    • 1970-01-01
    • 2017-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多