简体   繁体   English

从 .net-core 工具中取消子进程

[英]Disown a child process from .net-core tool

I have a tool which runs a ssh command so I can connect to the linux pc via reverse ssh.我有一个运行 ssh 命令的工具,所以我可以通过反向 ssh 连接到 linux pc。 When I want to update my tool, written in .Net Core 2.1 , the updater will stop the process of the tool and update the files.当我想更新用.Net Core 2.1编写的工具时,更新程序将停止该工具的进程并更新文件。 But by stopping the tool the ssh process, started before with Process.Start() , will be closed, too, as the ssh process is a child process from the tool.但是通过停止该工具,之前使用Process.Start()启动的 ssh 进程也将关闭,因为 ssh 进程是该工具的子进程。

Is there any way to "disown" the child ssh process from my tool so I wont loose connection while the updater runs?有什么方法可以从我的工具中“否认”子 ssh 进程,这样我就不会在更新程序运行时断开连接?

Running Ubuntu...运行 Ubuntu...

C#: C#:

case UssGatewayFunction.NPIConnectSSH:
    Log.Information("Connect to MyCompany via ssh");
    try
    {
        Process.Start("ssh", "-R 3521:localhost:22 -p 9450 myuser@1.2.3.4 sleep 30");
        response = new DefaultRes(true, UssGatewayFunction.NPIConnectSSH);
    }
    catch (Exception ex)
    {
        Log.Error($"Can't start ssh: {ex.Message}");
        response = new DefaultRes(false, UssGatewayFunction.NPIConnectSSH);
    }

Updater.sh:更新程序.sh:

systemctl stop gateway.service
if pgrep "gateway" >/dev/null 2>&1 ; then
        #here i loose the ssh connection
        pkill -x "init-gateway" || true
        pkill -x "gateway" || true
fi
#now just the extracting happens

What worked for me:什么对我有用:

 [DllImport("libc")]
 private static extern int system(string exec);
 
 system($"nohup {command} >/dev/null 2>&1 &");

Infos : system , nohup信息:系统nohup

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM