通过 SSH 连接到服务器并在后台运行脚本

问题描述 投票:0回答:1

问题

我正在尝试 ssh 到 ec2 实例并在后台运行我的 python 脚本,以便将其与 SSH 会话分离。这是我目前正在尝试的命令:

ssh -i {key} -o "StrictHostKeyChecking no" -q ec2-user@{ip} /bin/sh -c 'nohup env/bin/python main.py > log.out &'

这会导致以下错误:

nohup: missing operand

我尝试过的事情

我可以先 ssh 到服务器,然后运行 /bin/sh -c ... 命令,它工作正常。 nohup 是否需要不同的 ssh 参数才能正常工作?

local$ ssh ec2-user@{ip}
...
remote$ /bin/sh -c 'nohup env/bin/python main.py > log.out &'
amazon-web-services amazon-ec2 ssh nohup
1个回答
0
投票

以下命令有效:

ssh -i {key} -o {opts} -nf ec2-user@{ip} "nohup env/bin/python main.py > log.out &"

考虑了 Anon 和 Yuri 的评论后,我意识到我可能不需要额外的

sh -c
电话。对原始命令所做的唯一更改是:

  • -nf 选项

    启动命令后分离 SSH 会话

  • “nohup ...&”

    引用整个命令,以便它安全地传递到远程 shell

© www.soinside.com 2019 - 2024. All rights reserved.