如何使用 ssh 在后台运行命令并分离会话
我目前正在尝试 ssh 到远程计算机并运行脚本,然后让脚本运行的节点。下面是我的脚本。但是,当它运行时,脚本在计算机上成功运行,但 ssh 会话挂起。有什么问题吗?
ssh -x $username@$node 'rm -rf statuslist
mkdir statuslist
chmod u+x ~/monitor/concat.sh
chmod u+x ~/monitor/script.sh
nohup ./monitor/concat.sh &
exit;'
I'm currently trying to ssh into a remote machine and run a script, then leave the node with the script running. Below is my script. However, when it runs, the script is successfully run on the machine but ssh session hangs. What's the problem?
ssh -x $username@$node 'rm -rf statuslist
mkdir statuslist
chmod u+x ~/monitor/concat.sh
chmod u+x ~/monitor/script.sh
nohup ./monitor/concat.sh &
exit;'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在某些情况下,您想要在远程计算机/服务器上执行/启动某些脚本(将自动终止)并与服务器断开连接。
例如:在盒子上运行的脚本,执行时
我将使用以下命令:
@CKeven,您可以将所有这些命令放在一个脚本上,将其推送到远程服务器并按如下方式启动它:
希望这有效;-)
编辑:
您还可以使用
ssh user@host 'screen -S SessionName -d -m "/path/to/executable"'
创建一个分离的屏幕会话并在其中运行目标命令
There are some situations when you want to execute/start some scripts on a remote machine/server (which will terminate automatically) and disconnect from the server.
eg: A script running on a box which when executed
I would use the following command:
@CKeven, you may put all those commands on one script, push it to the remote server and initiate it as follows:
Hope this works ;-)
Edit:
You can also use
ssh user@host 'screen -S SessionName -d -m "/path/to/executable"'
Which creates a detached screen session and runs target command within it
您认为使用
screen
来实现此目的怎么样?您可以通过 ssh 运行screen
来启动命令 (concat.sh
),然后如果您想监视它,就可以返回到 screen 会话(可以很方便,具体取决于 concat 的作用)。更具体地说,请尝试以下操作:
您应该会发现提示符立即返回,并且
concat.sh
正在远程计算机上运行。我将解释一些选项:ssh -t
创建 TTY。屏幕需要这个。screen -dm
使其以“分离”模式启动。这就像您的目的的“背景”。-S
测试为您的屏幕会话命名。它是可选的,但推荐使用。现在,完成此操作后,您可以转到远程计算机并运行此命令:
这会将您附加到包含您的程序的屏幕会话。从那里你可以控制它、杀死它、查看它的输出等等。 Ctrl-A,然后 d 将使您脱离屏幕会话。
screen -ls
将列出所有正在运行的会话。What do you think about using
screen
for this? You could runscreen
via ssh to start the command (concat.sh
) and then you'd be able to return to the screen session if you wanted to monitor it (could be handy, depending on what concat does).To be more specific, try this:
You should find that the prompt returns immediately, and that
concat.sh
is running on the remote machine. I'll explain some of the options:ssh -t
makes a TTY. screen needs this.screen -dm
makes it start in "detached" mode. This is like "background" for your purposes.-S
testing gives your screen session a name. It is optional but recommended.Now, once you've done this, you can go to the remote machine and run this:
This will attach you to the screen session which contains your program. From there you can control it, kill it, see its output, and so on. Ctrl-A, then d will detach you from the screen session.
screen -ls
will list all running sessions.它可能是标准输入流。尝试
ssh -n ...
或ssh -f ...
。It could be the standard input stream. Try
ssh -n ...
orssh -f ...
.对我来说,只有这个有效:
当然,这取决于屏幕,并且可以让您稍后附加(如果您需要标准输入或标准输出)。当 my-script.sh 结束时,屏幕将自行终止。
For me, only this worked:
This, of course, depends on screen, and lets you attach later, if you ever want stdin or stdout. Screen will terminate itself when my-script.sh ends.
下面是一个更常见的决定,需要付出一些努力才能找到,它确实对我有用:
它将 $theStuff 命令列表发送到选项卡 No $theTabNumber of the screen session $theScreenSessionName preliminary create at the 'server' onpresent '用户'。
请注意后面的尾随空格
-X 东西\"
发送它是为了克服“东西”选项的故障。下一行中的空格和 $theStuff 通过“Enter”(^M) 按键附加。不要错过他们!
“这是一个测试”消息在初始终端中回显,并且 $theStuff 命令实际上在提到的屏幕/选项卡内执行。
Below is a much more common decision that required some efforts to find, and it really works for me:
It sends $theStuff list of commands to the tab No $theTabNumber of the screen session $theScreenSessionName preliminarily created at the 'server' on behalf of 'user'.
Please be aware of a trailing whitespace after
-X stuff \"
that is sent to overcome a 'stuff' option's glitch. The whitespace and $theStuff in the next line are appended by 'Enter' (^M) keystrokes. DON'T MISS 'EM!
The "this is a test" message is echoed in the initial terminal, and $theStuff commands are really executed inside the mentioned screen/tab.