免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: reyleon
打印 上一主题 下一主题

[系统管理] ssh 远程后台执行脚本疑惑,求解惑 [复制链接]

论坛徽章:
60
20周年集字徽章-20	
日期:2020-10-28 14:04:3015-16赛季CBA联赛之北京
日期:2016-07-06 15:42:0715-16赛季CBA联赛之同曦
日期:2016-06-12 10:38:0915-16赛季CBA联赛之佛山
日期:2016-05-27 11:54:56黄金圣斗士
日期:2015-12-02 11:44:35白银圣斗士
日期:2015-11-25 14:32:43白银圣斗士
日期:2015-11-23 12:53:352015亚冠之布里斯班狮吼
日期:2015-10-21 16:55:482015亚冠之首尔
日期:2015-09-01 16:46:052015亚冠之德黑兰石油
日期:2015-08-31 11:39:192015亚冠之萨济拖拉机
日期:2015-08-28 21:06:5315-16赛季CBA联赛之广东
日期:2016-07-12 14:58:53
11 [报告]
发表于 2013-01-21 12:21 |只看该作者
回复 10# blackold


    经过测试,下面两条命令只有第一条可以喔

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015年亚洲杯之朝鲜
日期:2015-03-13 22:47:33IT运维版块每日发帖之星
日期:2016-01-09 06:20:00IT运维版块每周发帖之星
日期:2016-03-07 16:27:44
12 [报告]
发表于 2013-01-21 12:25 |只看该作者
回复 11# reyleon


    这样啊。

    && 会生成新的 shell 进程,cmd1 和 cmd2 在此新的shell进程下执行。

猜测原因: ssh 要等待新 shell 进程的结束。

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015年亚洲杯之朝鲜
日期:2015-03-13 22:47:33IT运维版块每日发帖之星
日期:2016-01-09 06:20:00IT运维版块每周发帖之星
日期:2016-03-07 16:27:44
13 [报告]
发表于 2013-01-21 12:26 |只看该作者
懂的朋友来回答啊。

未测试。

论坛徽章:
2
技术图书徽章
日期:2013-10-30 14:37:252015年亚洲杯之伊朗
日期:2015-04-30 13:37:02
14 [报告]
发表于 2013-01-21 15:33 |只看该作者
@reyleon@blackold

My ssh client hangs when I try to logout after running a remote background job!
http://mywiki.wooledge.org/BashFAQ/063

论坛徽章:
60
20周年集字徽章-20	
日期:2020-10-28 14:04:3015-16赛季CBA联赛之北京
日期:2016-07-06 15:42:0715-16赛季CBA联赛之同曦
日期:2016-06-12 10:38:0915-16赛季CBA联赛之佛山
日期:2016-05-27 11:54:56黄金圣斗士
日期:2015-12-02 11:44:35白银圣斗士
日期:2015-11-25 14:32:43白银圣斗士
日期:2015-11-23 12:53:352015亚冠之布里斯班狮吼
日期:2015-10-21 16:55:482015亚冠之首尔
日期:2015-09-01 16:46:052015亚冠之德黑兰石油
日期:2015-08-31 11:39:192015亚冠之萨济拖拉机
日期:2015-08-28 21:06:5315-16赛季CBA联赛之广东
日期:2016-07-12 14:58:53
15 [报告]
发表于 2013-01-21 15:52 |只看该作者
回复 14# ajdan4q


    现在的问题是

ssh me@remotehost "cd /dir/ && sleep 120 >/dev/null 2>&1 &"

不立刻关闭连接,而

ssh me@remotehost "cd /dir/ ; sleep 120 >/dev/null 2>&1 &"

则会立刻关闭连接

&& 跟 ; 的区别,';' 是命令分隔符 , '&&' 是与逻辑操作符,只有它前面为真才会执行后面的命令。 && 在ssh中会有问题??

论坛徽章:
2
技术图书徽章
日期:2013-10-30 14:37:252015年亚洲杯之伊朗
日期:2015-04-30 13:37:02
16 [报告]
发表于 2013-01-21 16:01 |只看该作者
回复 15# reyleon

这样就可以了。
  1. ssh me@remotehost "{cd /dir/ && sleep 120} >/dev/null 2>&1 &"
复制代码
没加{这前,估计只是重定向了sleep命令,cd命令没有重定向。使用{}就可以把所有输出重定向,这样ssh就会立刻返回。

论坛徽章:
60
20周年集字徽章-20	
日期:2020-10-28 14:04:3015-16赛季CBA联赛之北京
日期:2016-07-06 15:42:0715-16赛季CBA联赛之同曦
日期:2016-06-12 10:38:0915-16赛季CBA联赛之佛山
日期:2016-05-27 11:54:56黄金圣斗士
日期:2015-12-02 11:44:35白银圣斗士
日期:2015-11-25 14:32:43白银圣斗士
日期:2015-11-23 12:53:352015亚冠之布里斯班狮吼
日期:2015-10-21 16:55:482015亚冠之首尔
日期:2015-09-01 16:46:052015亚冠之德黑兰石油
日期:2015-08-31 11:39:192015亚冠之萨济拖拉机
日期:2015-08-28 21:06:5315-16赛季CBA联赛之广东
日期:2016-07-12 14:58:53
17 [报告]
发表于 2013-01-21 16:06 |只看该作者
回复 16# ajdan4q


    这种{}方式我试过,不行的,报语法错误 bash: -c: line 1: syntax error: unexpected end of file;

   使用 ( cd /dir/ && sleep 120 ) >/dev/null 2>&1 & 则没问题

论坛徽章:
2
技术图书徽章
日期:2013-10-30 14:37:252015年亚洲杯之伊朗
日期:2015-04-30 13:37:02
18 [报告]
发表于 2013-01-21 16:10 |只看该作者
回复 17# reyleon


    那个是{}的问题
  1. ssh me@remotehost "{ cd /dir/ && sleep 120; } >/dev/null 2>&1 &"
复制代码

论坛徽章:
60
20周年集字徽章-20	
日期:2020-10-28 14:04:3015-16赛季CBA联赛之北京
日期:2016-07-06 15:42:0715-16赛季CBA联赛之同曦
日期:2016-06-12 10:38:0915-16赛季CBA联赛之佛山
日期:2016-05-27 11:54:56黄金圣斗士
日期:2015-12-02 11:44:35白银圣斗士
日期:2015-11-25 14:32:43白银圣斗士
日期:2015-11-23 12:53:352015亚冠之布里斯班狮吼
日期:2015-10-21 16:55:482015亚冠之首尔
日期:2015-09-01 16:46:052015亚冠之德黑兰石油
日期:2015-08-31 11:39:192015亚冠之萨济拖拉机
日期:2015-08-28 21:06:5315-16赛季CBA联赛之广东
日期:2016-07-12 14:58:53
19 [报告]
发表于 2013-01-21 16:34 |只看该作者
本帖最后由 reyleon 于 2013-10-18 12:12 编辑

回复 18# ajdan4q


    确实,OK了,应该是http://mywiki.wooledge.org/BashFAQ/063这个链接提到的问题了! 感谢。。。

以下为帖子中的内容:
  1. My ssh client hangs when I try to logout after running a remote background job!

  2. The following will not do what you expect:

  3.    ssh me@remotehost 'sleep 120 &'
  4.    # Client hangs for 120 seconds
  5. This is a "feature" of OpenSSH. The client will not close the connection as long as the remote end's terminal still is still in use -- and in the case of sleep 120 &, stdout and stderr are still connected to the terminal.

  6. The immediate answer to your question -- "How do I get the client to disconnect so I can get my shell back?" -- is to kill the ssh client. You can do this with the kill or pkill commands, of course; or by sending the INT signal (usually Ctrl-C) for a non-interactive ssh session (as above); or by pressing <Enter><~><.> (Enter, Tilde, Period) in the client's terminal window for an interactive remote shell.

  7. The long-term workaround for this is to ensure that all the file descriptors are redirected to a log file (or /dev/null) on the remote side:


  8.    ssh me@remotehost 'sleep 120 >/dev/null 2>&1 &'
  9.    # Client should return immediately
  10. This also applies to restarting daemons on some legacy Unix systems.


  11.    ssh root@hp-ux-box   # Interactive shell
  12.    ...                  # Discover that the problem is stale NFS handles
  13.    /sbin/init.d/nfs.client stop   # autofs is managed by this script and
  14.    /sbin/init.d/nfs.client start  # killing it on HP-UX is OK (unlike Linux)
  15.    exit
  16.    # Client hangs -- use Enter ~ . to kill it.
  17. Please note that allowing root to log in over SSH is a very bad security practice. If you must do this, then create a single script that does all the commands you want, with no command line options, and then configure the sudoers file to grant a single user the right to run the mentioned script with no password required. This will ensure that you know which commands need run regularly, and that if the regular account is compromised, the damage which can be incurred is quantified.

  18. The legacy Unix /sbin/init.d/nfs.client script runs daemons in the background but leaves their stdout and stderr attached to the terminal (and they don't fully self-daemonize). The solution is either to fix the Unix vendor's broken init script, or to kill the ssh client process after this happens. The author of this article uses the latter approach.
复制代码

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015年亚洲杯之朝鲜
日期:2015-03-13 22:47:33IT运维版块每日发帖之星
日期:2016-01-09 06:20:00IT运维版块每周发帖之星
日期:2016-03-07 16:27:44
20 [报告]
发表于 2013-01-21 18:17 |只看该作者
回复 14# ajdan4q


    谢谢分享!

    学习!

使用 { } 和 () 都可以。

不立即退出 ssh 的原因应该就是等待命令的输出,或者说 ssh 仍在使用终端。

谢谢!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP