当某些后台进程仍在运行时,命令“退出"不起作用 [英] Command 'exit' doesn't work when some background process is still piping out

查看:142
本文介绍了当某些后台进程仍在运行时,命令“退出"不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

'exit'命令不适用于我的情况,我也不知道为什么:

The 'exit' command doesn't work for my case, and I don't get it why:

ssh user@mysever <<'HEREDOC'
  echo "1. Running PM2 log..."
  pm2 log &
  echo "2. PM2 log is now in background"
  exit
  echo "3. Won't be here"
HEREDOC

echo "4. Out."

即使使用上面的"exit"命令,我也无法终止ssh管道.

I can't terminate the ssh pipeline, even with the 'exit' command above.

我可以看到第一个回声,第二个回声;当然没有第三回声;并卡住了.我期望可以看到第四回声的行为.我必须按Ctrl + C,然后才能看到第四个回声.

I can see the first echo, second echo; no third echo of course; and stuck. I expected the behaviour that I can see the fourth echo. I have to press Ctrl+C, and I see the 4th echo after that.

推荐答案

简短的答案:您应该重定向pm2进程的标准输入,输出和错误:

Short answer: You should redirect the standard input, output, and error for the pm2 process:

pm2 log < /dev/null > /dev/null 2>&1 &

这将防止远程ssh服务器在pm2退出之前将会话保持打开状态.

This will prevent the remote ssh server from holding the session open until pm2 exits.

更长的答案:

ssh user@mysever <<'HEREDOC'
    ...
    pm2 log &

以这种方式运行ssh时,远程ssh服务器将启动远程用户外壳程序的副本以处理会话.为了中继远程会话的输入和输出,远程ssh服务器将分配TTY或一组管道.然后,它将TTY或管道设置为Shell进程的标准输入,输出和错误.

When you run ssh in this fashion, the remote ssh server will launch a copy of the remote user's shell to handle the session. To relay input and output from the remote session, the remote ssh server will allocate either a TTY or a set of pipes. Then it sets the TTY or pipes as the standard input, output, and error for the shell process.

因此,在远程系统上,您具有TTY或一组将远程Shell进程连接到SSH服务器进程的管道.外壳程序调用的任何命令都将继承TTY或管道集作为命令的标准输入等.(除非您使用外壳程序功能来重定向标准文件句柄).

So, on the remote system, you have either a TTY or a set of pipes connecting the remote shell process to the SSH server process. Any commands invoked by the shell will inherit the TTY or set of pipes as the command's standard input etc. (unless you use shell features to redirect the standard file handles).

您可能认为ssh服务器将在远程Shell进程退出时终止会话.但是,事实并非如此. ssh服务器在读取TTY或连接到会话标准输出的管道上的文件结束条件时终止会话.

You might think that the ssh server will terminate the session when the remote shell process exits. But that's not what happens. The ssh server terminates the session when it reads an end-of-file condition on the TTY or the pipe connected to the standard output for the session.

在您的情况下,您正在远程系统上调用此pm2命令,并且该命令继承了远程会话的标准输出.只要该程序在运行,远程ssh服务器就不会在标准输出管道上获得EOF,也不会终止会话.

In your case, you're invoking this pm2 command on the remote system, and it's inheriting the remote session's standard output. As long as this program is running, the remote ssh server won't get an EOF on the standard output pipe and it won't terminate the session.

解决方法是重定向pm2进程的输入,以便它不继承连接到ssh服务器的标准句柄:

The fix is to redirect input for the pm2 process so it doesn't inherit the standard handles that are connected to the ssh server:

pm2 log < /dev/null > /dev/null 2>&1 &

如果要捕获输出,可以重定向到文件而不是/dev/null.我认为严格来说只有标准输出重定向是必需的,但是您也应该确保标准输入和错误都重定向.

You could redirect to a file instead of /dev/null if you want to capture the output. I think only the standard output redirection is strictly necessary, but you should redirect standard input and error as well just to be sure.

这篇关于当某些后台进程仍在运行时,命令“退出"不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆