个人经验分享:nohup?screen?tmux?谁来保证我的远程会话?

>nohup
说到这个工具,不管是unix还是linux上面都用到,常作为简单的终端复用命令,我们这里简单的演示下

我们常常在管理远程服务器的时候特别是这些unix-like系列的系统都会用到ssh远程登录,但是我们有时候例如要装一个mysql编译的时候,如果突然网络中断….我想你想死的新都有了….你可能会认为服务器会帮我们安装的……其实不会嗯服务器当发现网络终端会发送一个SIGHUP的指令此时你的mysql进程也会被结束掉…..我们来看下原理

我刚刚又用SSH连接了一个会话,此时我们可以用ps -ef|grep top来观察一下上面的那个会话的子PID和父PID,我们可以看到分别是3464和3220,如果想查看下进程之间的详细关系的话可以用pstree -H PID来显示出来,看到我们运行的top命令就是在当前SHELL中的子进程,所以当前的SHELL的进程为3220,此时把第一个会话给关掉

确定,关掉会话,也就是窗口关闭了

此时的情况大家就知道了吧,也就是说如果这里是mysql的话情况一样….也许没经验的都不知道中断了后该怎么办?重新装?还是该删除一些文件?在哪里删除等等这些问题……为了解决这样的问题我们可以用nohup里阻止系统接收SIGHUP指令,我们再次打开一个终端

我们可以看下语法很简单直接nohup [common] [参数] &,这里必须让这命令后台执行,其实我们看到输入也知道了它的原理是把标准输入和标准输入都写到nohup.out零时文件中了,我们关闭掉它然后再再另一个终端查看,因为top读取TTY的一些东西会失败所以我们换脚本,脚本的内容是循环的输入i love you,ok我们关掉窗口试试看

此时就会发现,这样就可以保证我们的程序不会意外的中断了….其实写这个是因为我看到百度上的一些技术博客写的说是nohup只是简单的把进程拿到后台去……这里用实验给大家也证明了下….好这个nohup就讲到这里了
>screen

这个应该算是可以拟补nohup的简单了,screen在readhat4左右吧就以及在iso的文件中了,大家自己安装下嗯

这个screen老牛逼了,可以在一个会话中创建多个screen窗口,每一个窗口就像操作真实的连接窗口一样样儿的…所以非常有意思哦,但是里面有很几个快捷键需要大家记住
C-a ?显示所有键绑定信息C-a w显示所有窗口列表C-a C-a切换到之前显示的窗口C-a c创建一个新的运行shell的窗口并切换到该窗口C-a n切换到下一个窗口C-a p切换到前一个窗口(与C-a n相对)C-a 0..9切换到窗口0..9C-a a发送 C-a到当前窗口C-a d暂时断开screen会话C-a k杀掉当前窗口C-a [进入拷贝/回滚模式
就是这些了,C代表的是ctrl键,后面的a啊就是a键而后面的是先按了ctrl+a再按后面的键盘才有效,最重要的是ctrl+a+d这个保存当前的screen终端

然后我们退出窗口再次登录试试

当你打开screen的时候会会启动一个子终端,pts-0这些东西,你可以开无限个,同时在不同的终端执行多个指令,然后突然关闭窗口再次进入的时候用screen -ls来查看当前的终端列表,会看到有一个是Detached也就是分开的的,不是真的死了嗯只是暂时离开终端了,可以用-r参数后面跟上进程号就可以恢复所有的screen终端了,里面执行的命令也不会丢失依然再执行中
>tmux(常常用于freebsd等unix系统,但是linux也可以只是比screen复杂点儿)
官网:http://tmux.sourceforge.net/
在远程主机上运行一些长时间任务时,我们通常选用screen命令保持会话,以便下次登录继续作业。可screen有个问题,ssh到远程主机,打开screen,开始工作。还没退出screen会话,这时,由于网络原因掉线,非主动退出远程主机。再次连上主机会发现之前建立的screen会话已经不能打开,遇到没有保存的工作就惨了。
而使用tmux不会出现上述问题,就算在tmux会话非正常退出主机,下次登录时依然可以找到
tmux ls    #查看tmux创建的会话
tmux a -t 会话名    #打开已存在的会话
c-b d    #退出会话,可再次连上
c-b !    #关闭所有窗口
注:c-b d 是按下ctrl+b之后,再按d。

本文来自:银河网安官网[WWW.YHSAFE.NET] 请尊重作者原创,转载时带上版权信息.详情参考:http://www.yhsafe.net/nohup_screen_tmux_manager_session.html

13 Responses so far.

  1. Quest Bars says:
    Great post. I’m facing some of these issues as well..
  2. quest bars says:
    Hi, everything is going nicely here and ofcourse every one is sharing facts,
    that’s in fact fine, keep up writing.
  3. Simply want to say your article is as astounding. The clarity in your post is just spectacular and i could
    assume you are an expert on this subject. Fine with your permission let me to grab
    your feed to keep updated with forthcoming post.
    Thanks a million and please keep up the enjoyable work.
  4. That is a really good tip especially to those new to the blogosphere.
    Brief but very precise information… Appreciate your sharing this one.
    A must read article!
  5. This website was… how do you say it? Relevant!! Finally I’ve
    found something which helped me. Many thanks!
  6. My brother suggested I might like this web site. He was entirely
    right. This post truly made my day. You can not imagine simply how much
    time I had spent for this info! Thanks!
  7. You really make it seem so easy with your presentation but I find this topic to be
    really one thing which I think I’d by no means
    understand. It kind of feels too complex and extremely vast for me.
    I am looking forward for your next post, I will attempt to get the cling of it!
  8. Its not my first time to go to see this web page,
    i am visiting this web site dailly and take good
    facts from here everyday.
  9. Hey! This is my 1st comment here so I just wanted to give a quick shout out
    and tell you I truly enjoy reading your posts. Can you
    recommend any other blogs/websites/forums that cover the same subjects?

    Thanks a ton!

  10. Hello! Someone in my Myspace group shared this website with us so I came to look it over.
    I’m definitely loving the information. I’m bookmarking and will be
    tweeting this to my followers! Exceptional blog and outstanding design.
  11. Thanks for finally writing about >个人经验分享:
    nohup?screen?tmux?谁来保证我的远程会话? – Huang
    Shiyang <Liked it!
  12. This design is incredible! You certainly know how to keep a reader amused.
    Between your wit and your videos, I was almost moved
    to start my own blog (well, almost…HaHa!) Wonderful job.
    I really enjoyed what you had to say, and more than that, how you presented it.

    Too cool!

  13. Hi there, after reading this remarkable article i am also cheerful to share my familiarity here with mates.

LEAVE A COMMENT