这是一个后续的问题,你如何使用ssh在一个shell脚本?的问题。如果我想在远程机器上执行在后台运行的命令,如何返回ssh命令?当我试图在命令末尾只包含&号时,它就挂起了。命令的确切形式如下所示:

ssh user@target "cd /some/directory; program-to-execute &"

什么好主意吗?需要注意的一件事是,登录到目标机器总是产生一个文本横幅,我设置了SSH密钥,所以不需要密码。


当前回答

我认为这是你需要的: 首先,您需要在您的机器上安装sshpass。 然后你可以编写自己的脚本:

while read pass port user ip; do
sshpass -p$pass ssh -p $port $user@$ip <<ENDSSH1
    COMMAND 1
    .
    .
    .
    COMMAND n
ENDSSH1
done <<____HERE
    PASS    PORT    USER    IP
      .      .       .       .
      .      .       .       .
      .      .       .       .
    PASS    PORT    USER    IP    
____HERE

其他回答

我只是想展示一个你可以剪切和粘贴的工作示例:

ssh REMOTE "sh -c \"(nohup sleep 30; touch nohup-exit) > /dev/null &\""

首先遵循以下步骤:

以用户A登录A,生成一对认证密钥。不要输入密码:

a@A:~> ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/a/.ssh/id_rsa): 
Created directory '/home/a/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/a/.ssh/id_rsa.
Your public key has been saved in /home/a/.ssh/id_rsa.pub.
The key fingerprint is:
3e:4f:05:79:3a:9f:96:7c:3b:ad:e9:58:37:bc:37:e4 a@A

现在使用ssh创建一个目录~/。(目录可能已经存在,这没有问题):

a@A:~> ssh b@B mkdir -p .ssh
b@B's password: 

最后将a的新公钥追加到b@B:。Ssh /authorized_keys最后一次输入b的密码:

a@A:~> cat .ssh/id_rsa.pub | ssh b@B 'cat >> .ssh/authorized_keys'
b@B's password: 

从现在开始,你可以以B的身份从A以A的身份登录到B,无需密码:

a@A:~> ssh b@B

这样就可以不用输入密码了

ssh b@B "cd /some/目录;program-to-execute &”

If you run remote command without allocating tty, redirect stdout/stderr works, nohup is not necessary. ssh user@host 'background command &>/dev/null &' If you use -t to allocate tty to run interactive command along with background command, and background command is the last command, like this: ssh -t user@host 'bash -c "interactive command; nohup backgroud command &>/dev/null &"' It's possible that background command doesn't actually start. There's race here: bash exits after nohup starts. As a session leader, bash exit results in HUP signal sent to nohup process. nohup ignores HUP signal. If 1 completes before 2, the nohup process will exit and won't start the background command at all. We need to wait nohup start the background command. A simple workaroung is to just add a sleep: ssh -t user@host 'bash -c "interactive command; nohup backgroud command &>/dev/null & sleep 1"'

这个问题在几年前就被提出和回答了,我不知道openssh的行为从那时起是否发生了变化。我在测试: OpenSSH_8.6p1, OpenSSL 1.1.1g FIPS 21 Apr 2020

我在一年前写的一个程序中遇到过这个问题——结果发现答案相当复杂。您将需要使用nohup以及输出重定向,正如维基百科中关于nohup的文章所解释的那样,为了方便您,此处复制了该文章。

Nohuping后台工作是为了 通过SSH登录时有用的例子, 由于后台作业可能导致 Shell在退出时挂起 条件[2]。这个问题也可以 通过重定向这三个方面来克服 I / O流: 没有myprogram > foo。出2> foo。Err < /dev/null &

如果你不能/不能保持连接打开,你可以使用屏幕,如果你有权限安装它。

user@localhost $ screen -t remote-command
user@localhost $ ssh user@target # now inside of a screen session
user@remotehost $ cd /some/directory; program-to-execute &

分离屏幕会话:ctrl-a d

列出屏幕会话:

screen -ls

重新连接一个会话:

screen -d -r remote-command

注意,screen还可以在每个会话中创建多个shell。使用tmux也可以达到类似的效果。

user@localhost $ tmux
user@localhost $ ssh user@target # now inside of a tmux session
user@remotehost $ cd /some/directory; program-to-execute &

卸载tmux会话:ctrl-b d

列出屏幕会话:

tmux list-sessions

重新连接一个会话:

tmux attach <session number>

默认的tmux控制键'ctrl-b'使用起来有些困难,但有几个tmux配置的示例,您可以尝试。