首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从.net-core工具中断开子进程

从.net-core工具中断开子进程
EN

Stack Overflow用户
提问于 2019-01-21 23:03:25
回答 1查看 186关注 0票数 1

我有一个运行ssh命令的工具,这样我就可以通过反向ssh连接到linux pc。当我想要更新用.Net Core 2.1编写的工具时,更新程序会停止工具的进程并更新文件。但是通过停止该工具,之前使用Process.Start()启动的ssh进程也将关闭,因为ssh进程是该工具的一个子进程。

有没有办法从我的工具中“断绝”子ssh进程,这样我就不会在更新程序运行时断开连接?

运行Ubuntu...

C#:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2020-10-07 06:57:25

对我起作用的是:

代码语言:javascript
复制
 [DllImport("libc")]
 private static extern int system(string exec);
 
 system($"nohup {command} >/dev/null 2>&1 &");

信息:systemnohup

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54292654

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档