【问题标题】:Running Python Script on Remote Server via SSH通过 SSH 在远程服务器上运行 Python 脚本
【发布时间】:2017-07-31 15:29:55
【问题描述】:

我正在尝试在 Digital Ocean (DO) Droplet (Ubuntu 16) 上运行此 Python 脚本: https://github.com/instabot-py/instabot.py

虽然我可以运行python3 example.py,但如果我终止用于通过 SSH 连接到 DO Droplet 的终端窗口,进程就会停止。

有没有办法运行这个 Python 脚本并在 Python 脚本仍在运行的情况下断开与 DO Droplet 的连接?

【问题讨论】:

标签: python linux ssh


【解决方案1】:

使用nohup后台运行程序

nohup python3 example.py > /dev/null 2>&1 &

或者您可以使用screen 来执行此操作:

$ screen
$ python3 example.py
To detach the current session, you can press Ctrl+A and then press D.
To re-attach previous session, use command `screen -r`

screen 命令比nohup 更强大。

【讨论】:

    【解决方案2】:

    您可以在 linux 中使用screen 继续在后台执行进程,即使您与服务器断开连接也是如此。 同样可以使用tmux

    this 问题的答案很可能就是您正在寻找的。​​p>

    【讨论】:

      【解决方案3】:

      你可以试试这些python库来实现这个

      http://www.paramiko.org/

      https://pypi.python.org/pypi/spur

      或者您也可以使用 tmux 在远程服务器上运行命令,即使您退出 shell,您的 tmux seisson 也会在后台运行。您甚至可以在几天后加入该 tmux 会话并继续执行命令

      【讨论】:

        【解决方案4】:

        我们使用如下格式来执行远程脚本 ssh -t -t user@host.com 'nohup /path/to/scrip.py'

        【讨论】:

          猜你喜欢
          • 2016-11-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-09-04
          • 1970-01-01
          • 1970-01-01
          • 2010-11-03
          • 1970-01-01
          相关资源
          最近更新 更多