【问题标题】:Execute command with SSH.NET without blocking and keep it running after closing the connection使用 SSH.NET 执行命令而不阻塞,并在关闭连接后保持运行
【发布时间】:2021-08-11 19:26:37
【问题描述】:

我正在使用以下代码:

using (var client = new SshClient(host, user, pass))
{
    client.Connect();

    var terminal = client.RunCommand("/home/my-script.sh");
}

client.RunCommand 挂起,因为我正在使用的命令正在创建一个线程。我希望执行命令而不是等待线程完成。我该怎么点呢?

所以总结一下,我想:

  • 发送创建后台工作(线程)的命令
  • 在后台工作完成时,我应该能够关闭会话/连接以继续后台工作

参考:https://csharp.hotexamples.com/examples/Renci.SshNet/SshClient/RunCommand/php-sshclient-runcommand-method-examples.html

【问题讨论】:

    标签: c# .net shell ssh ssh.net


    【解决方案1】:

    这不是真正的 SSH.NET 问题,而是一个通用的 SSH/shell 问题。

    How to make a program continue to run after log out from ssh?

    所以应该这样做:

    client.RunCommand("nohup /home/my-script.sh > foo.out 2> foo.err < /dev/null &");
    

    【讨论】:

    • 非常感谢。
    猜你喜欢
    • 2013-07-05
    • 2012-06-14
    • 1970-01-01
    • 2012-06-17
    • 1970-01-01
    • 1970-01-01
    • 2013-09-26
    • 1970-01-01
    • 2018-08-20
    相关资源
    最近更新 更多