8

ssh远程执行脚本后无返回的解决方法

 3 years ago
source link: https://www.lujun9972.win/blog/2018/02/12/ssh%E8%BF%9C%E7%A8%8B%E6%89%A7%E8%A1%8C%E8%84%9A%E6%9C%AC%E5%90%8E%E6%97%A0%E8%BF%94%E5%9B%9E%E7%9A%84%E8%A7%A3%E5%86%B3%E6%96%B9%E6%B3%95/index.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

ssh远程执行脚本后无返回的解决方法

今天遇见一件很诡异的事情. 使用ssh远程执行脚本,脚本执行完毕后ssh却并没有返回.

为了取数据库执行计划,我写了个取执行计划的脚本上传远程服务器,然后用ssh远程执行该脚本:

ssh [email protected] ". ~/.profile;ksh /tmp/getDBSQLV10.sh"

脚本执行完毕后,ssh却不知为什么一直卡死,并没有返回。

一开始以为是脚本陷入死循环无法终止,然而登陆服务器用ps命令查询,发现脚本已经执行完毕

# 登陆服务器
ssh [email protected]
# 通过ps命令发现已经没有脚本进程了
$ ps -elf |grep get
200001 A  mbinst1 25231554 54460722   0  60 20 886180480   256 f1000a02000e12b8 15:52:58  pts/1  0:00 grep get

但是ssh连接依然存在

$ ps -elf |grep ssh
40001 A  mbinst1 48103636 25559542   0  60 20 a46bac480  1584          15:44:46      -  0:00 sshd: mbinst1@notty
240001 A     root 62783664  2359730   0  60 20 e665ee480  1340 f1000e000ecd1cc8 15:52:48      -  0:00 sshd: mbinst1 [priv]
240001 A     root  2359730  1835420   0  60 20 e72764480  1324            Aug 28      -  0:00 /usr/sbin/sshd
240001 A     root 25559542  2359730   0  60 20 846e0c480  1228 f1000e005f718cc8 15:44:45      -  0:00 sshd: mbinst1 [priv]
200001 A  mbinst1 49283456 54460722   0  60 20 a86420480   256 f1000a02000e12b8 15:56:59  pts/1  0:00 grep ssh
40001 A  mbinst1 58458534 62783664   0  60 20 ec5a64480  1468          15:52:48      -  0:00 sshd: mbinst1@pts/1

从上面可以看出,一共存在两个ssh连接,其中 sshd: mbinst1@pts/1 就是当前正在操作的登陆会话,而 sshd: mbinst1@notty 为ssh远程执行命令的会话(这里@后面的是终端名,如果是本机登陆,则会显示为ttyN,如果是通过ssh远程登陆则会显示成pts/N,如果是通过sftp/scp/ssh远程执行命令创建的连接则会显示notty)

猜想,出现这种情况的原因是不是跟没有分配伪终端有关,抱着试一试的态度把命令改成

ssh -t [email protected] ". ~/.profile;ksh /tmp/getDBSQLV10.sh"

强制让ssh分配伪终端,发现居然真的正常退出了。

目前还无法理解原理,只能推测是跟ksh有关,求各位大神解释。

PS: 服务器是AIX6.1的操作系统,客户端是Redhat6.7


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK