如何使用 ssh 在后台运行命令并分离会话

发布于 2024-08-08 13:29:54 字数 393 浏览 7 评论 0原文

我目前正在尝试 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

时光病人 2024-08-15 13:29:54

在某些情况下,您想要在远程计算机/服务器上执行/启动某些脚本(将自动终止)并与服务器断开连接。

例如:在盒子上运行的脚本,执行时

  1. 获取模型并将其复制到远程服务器
  2. 创建一个用于使用模型运行模拟的脚本并将其推送到服务器
  3. 在服务器上启动脚本并断开连接
  4. 因此脚本的职责开始是在服务器中运行模拟,完成后(需要几天时间才能完成)将结果复制回客户端。

我将使用以下命令:

ssh remoteserver 'nohup /path/to/script `</dev/null` >nohup.out 2>&1 &'

@CKeven,您可以将所有这些命令放在一个脚本上,将其推送到远程服务器并按如下方式启动它:

echo '#!/bin/bash  
rm -rf statuslist  
mkdir statuslist  
chmod u+x ~/monitor/concat.sh  
chmod u+x ~/monitor/script.sh  
nohup ./monitor/concat.sh &  
' > script.sh

chmod u+x script.sh

rsync -azvp script.sh remotehost:/tmp

ssh remotehost '/tmp/script.sh `</dev/null` >nohup.out 2>&1 &'

希望这有效;-)

编辑:
您还可以使用
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

  1. takes a model and copies it to a remote server
  2. creates a script for running a simulation with the model and push it to server
  3. starts the script on the server and disconnect
  4. The duty of the script thus started is to run the simulation in the server and once completed (will take days to complete) copy the results back to client.

I would use the following command:

ssh remoteserver 'nohup /path/to/script `</dev/null` >nohup.out 2>&1 &'

@CKeven, you may put all those commands on one script, push it to the remote server and initiate it as follows:

echo '#!/bin/bash  
rm -rf statuslist  
mkdir statuslist  
chmod u+x ~/monitor/concat.sh  
chmod u+x ~/monitor/script.sh  
nohup ./monitor/concat.sh &  
' > script.sh

chmod u+x script.sh

rsync -azvp script.sh remotehost:/tmp

ssh remotehost '/tmp/script.sh `</dev/null` >nohup.out 2>&1 &'

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

無心 2024-08-15 13:29:54

您认为使用 screen 来实现此目的怎么样?您可以通过 ssh 运行 screen 来启动命令 (concat.sh),然后如果您想监视它,就可以返回到 screen 会话(可以很方便,具体取决于 concat 的作用)。

更具体地说,请尝试以下操作:

ssh -t $username@$node screen -dm -S testing ./monitor/concat.sh

您应该会发现提示符立即返回,并且 concat.sh 正在远程计算机上运行。我将解释一些选项:

  • ssh -t 创建 TTY。屏幕需要这个。
  • screen -dm 使其以“分离”模式启动。这就像您的目的的“背景”。
  • -S 测试为您的屏幕会话命名。它是可选的,但推荐使用。

现在,完成此操作后,您可以转到远程计算机并运行此命令:

screen -r testing

这会将您附加到包含您的程序的屏幕会话。从那里你可以控制它、杀死它、查看它的输出等等。 Ctrl-A,然后 d 将使您脱离屏幕会话。 screen -ls 将列出所有正在运行的会话。

What do you think about using screen for this? You could run screen 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:

ssh -t $username@$node screen -dm -S testing ./monitor/concat.sh

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:

screen -r testing

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.

甜警司 2024-08-15 13:29:54

它可能是标准输入流。尝试ssh -n ...ssh -f ...

It could be the standard input stream. Try ssh -n ... or ssh -f ....

草莓酥 2024-08-15 13:29:54

对我来说,只有这个有效:

screen -dmS name sh my-script.sh

当然,这取决于屏幕,并且可以让您稍后附加(如果您需要标准输入或标准输出)。当 my-script.sh 结束时,屏幕将自行终止。

For me, only this worked:

screen -dmS name sh my-script.sh

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.

情深已缘浅 2024-08-15 13:29:54

下面是一个更常见的决定,需要付出一些努力才能找到,它确实对我有用:

#!/usr/bin/bash
theScreenSessionName="test"
theTabNumber="1"
theStuff="date; hostname; cd /usr/local; pwd; /usr/local/bin/top"

echo "this is a test"

ssh -f user@server "/usr/local/bin/screen -x $theScreenSessionName -p $theTabNumber -X stuff \" 
$theStuff
\""

它将 $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:

#!/usr/bin/bash
theScreenSessionName="test"
theTabNumber="1"
theStuff="date; hostname; cd /usr/local; pwd; /usr/local/bin/top"

echo "this is a test"

ssh -f user@server "/usr/local/bin/screen -x $theScreenSessionName -p $theTabNumber -X stuff \" 
$theStuff
\""

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文