Bash shell脚本在后台使用ssh远程运行命令whitnohup,但无法关闭此ssh进程。但我使用的屏幕都是完美的

Bash shell脚本在后台使用ssh远程运行命令whitnohup,但无法关闭此ssh进程。但我使用的屏幕都是完美的,bash,shell,ssh,Bash,Shell,Ssh,我使用shell脚本运行ssh命令在后台远程执行cammand whit nohup,但是ssh connect不能退出,shell脚本也不能退出。为什么,这是我想知道的 当我使用屏幕命令是完美的 我发现当我使用nohup执行命令时,命令父进程ID是bash进程ID的ssh连接carete。当我终止ssh连接时,commadn父进程ID是1(init) 当我使用screen时,命令parent process ID是bash process ID的screen carete。screen par

我使用shell脚本运行ssh命令在后台远程执行cammand whit nohup,但是ssh connect不能退出,shell脚本也不能退出。为什么,这是我想知道的

当我使用屏幕命令是完美的

我发现当我使用nohup执行命令时,命令父进程ID是bash进程ID的ssh连接carete。当我终止ssh连接时,commadn父进程ID是1(init)

当我使用screen时,命令parent process ID是bash process ID的screen carete。screen parent process ID是1


对不起,我的英语很差

如果您的某个应用程序仍然连接到它进行终端输入/输出,那么可能不允许退出
ssh

您可以尝试重定向后台进程的输入:

nohup sh /tmp/cmd.sh </dev/null &

您运行的ssh命令是什么?如下所示:root@localhost/tmp/cmd.sh中的“nohup sh/tmp/cmd.sh&”是while循环,while为true;do:;完成。不会退出。请在ssh命令中添加“-n”标志。man ssh了解更多详细信息。我尝试了一切,发现原因在于
nohup
命令是永不退出。使用
屏幕
正常。
nohup sh/tmp/cmd.sh/dev/null 2>&1&
可以工作。谢谢。但我想知道为什么。可以帮助解释使用
屏幕
运行这个程序实际上比
nohup
要好。在
nohup
中,commad is可以是另一个命令,只要该命令不退出即可。ssh连接将不会退出。@Txxk,因为我假设,只要应用程序仍在访问由其创建的虚拟stdin/stdout/stderr(实际上是管道),远程端ssh调用的虚拟终端就可能不会退出。即使应用程序在后台,它仍然能够访问stdout/stderr,因此通过重定向到null或其他设备或文件来断开应用程序与stdout/stderr的连接将允许终端退出。
nohup sh /tmp/cmd.sh </dev/null >/dev/null 2>&1 &
while true; do sleep 10; done