您的位置:首页 > 其它

那些年,和ssh战斗的日子

2013-08-13 13:20 197 查看
linux开发,远程执行命令是一个绕不过去的问题。需要启动程序,一直执行,解决方法有三个:

nohup
screen
daemon
现在将问题简单升级一下,需要在一台中控机上启动其它服务器上所有的程序,怎么解决?
问题可以分解为密码校验、远程执行命令两个部分。
密码校验可以使用互信的方式,具体参考ssh。

现在主要说的是如何在远程执行一个程序,并且让该程序在后台运行。
一般情况下可以这样:
ssh -pxxx user@host "cd path && nohup xxx &";
看官可以试试,这样执行的结果是终端处于等待状态,需要输入Ctrl+c才能继续执行。

怎么破?
man ssh
     -t      Force pseudo-tty allocation.  This can be used to execute arbitrary screen-based programs on a remote machine,

             which can be very useful, e.g., when implementing menu services.  Multiple -t options force tty allocation, even

             if ssh has no local tty.

ssh -pxxx -t user@host "cd path && nohup xxx &";
这样,对于大多数的情况都能搞定了,但是,总是有一个但是,对于python、或者执行较慢的程序在远程的机器上根本无法启动程序!

怎么破?
man ssh
     -f      Requests ssh to go to background just before command execution.  This is useful if ssh is going to ask for pass-

             words or passphrases, but the user wants it in the background.  This implies -n.  The recommended way to start

             X11 programs at a remote site is with something like ssh -f host xterm.

ssh -pxxx -nf user@host "cd path && nohup xxx &";
的确搞定了,测试没有发现什么异常的情况。

但是,又是但是,这样的话,总是存在一个父进程(启动nohup的bash的父进程),这个问题能否避免?
对于该问题,笔者认为一个比较好的解决策略是使用daemon实现程序,这样,大家的烦恼都少了。
至于怎么实现一个守护进程,请移步:
http://blog.csdn.net/bladecoder/article/details/8636316

不管什么语言,原理是一致的。

P.S.
千万不要百度,结果实在太差,浪费生命浪费对生活的期望。。。
Google吧,没有就bing
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: