码迷,mamicode.com
首页 > 系统相关 > 详细

screen & tmux 技巧:linux后台运行程序

时间:2015-07-27 19:12:25      阅读:952      评论:0      收藏:0      [点我收藏+]

标签:screen   commands   logging   后台   

通俗简介:

screen 简单应用:

screen myprogram     // 通过screen启动一个程序, 或者screen -L myprogram: turns on logging to ~/screenlog.0
ctr+a 然后再按一下 d   // 使程序在后台运行,即detach
screen -ls           // 列出在screen后台运行的程序,以及ID(例如4491.pts-2.localhost)
screen -r ID         // 恢复运行程序到terminal,例如:screen -r 4491
exit //退出当前screen会话(同 ctrl+a 再按 k),有时候会打开很多screen,可以通过exit退出。

多窗口管理:

如果在某一个终端中运行了多次screen,即有嵌套的多层screen,则 ctrl+a 再按 w 可查看当前终端下的所有screen窗口,* 号 表示的是当前会话所在的窗口,然后 ctrl+a 再按 0~9 即可切换到某个对应窗口。

介绍screen的很好的文章,just read it:
http://www.ibm.com/developerworks/cn/linux/l-cn-screen/


复杂点的应用如下:


One way:

When you use screen you need to detach with CTRL+A+D before you exit ssh.

Alternatively, if you want to run a process directly with screen you can use

screen -dmSL [session name] [commands]
-d: starts a screen session and immediately detaches from it
-m: forces creating a new screen session
-S: lets you give the session a name
-L: turns on logging to ~/screenlog.0

example:

screen -dmSL myalias myscript.sh

You can then either:

resume later using screen -x myalias

or

check the log file less -r ~/screenlog.0



Another way:

Screen Command Example 1: Execute a command (or shell-script), and detach the screen

$ screen unix-command-to-be-executed
$ screen ./unix-shell-script-to-be-executed

Once you’ve used the screen command, you can detach it from the terminal using any one of the following method.

Screen Detach Method 1: Detach the screen using CTRL+A d

When the command is executing, press CTRL+A followed by d to detach the screen.

Screen Detach Method 2: Detach the screen using -d option

When the command is running in another terminal, type the command as following.

$ screen -d SCREENID

Screen Command Example 2: List all the running screen processes

You can list all the running screen processes using screen -ls command.

For example:

On terminal 1 you did the following:

$ screen ./myscript.sh

From terminal 2 you can view the list of all screen processes. You can also detach it from terminal 2 as shown below.

$ screen -ls
There is a screen on:
    4491.pts-2.localhost    (Attached)
1 Socket in /var/run/screen/S-i5.
$ screen -d 4491.pts-2.localhost   
[4491.pts-2.localhost   detached.]

Screen Command Example 3: Attach the Screen when required

You can attach the screen at anytime by specifying the screen id as shown below. You can get the screen id from the “screen -ls” command output.

$ screen -r 4491.pts-2.FC547





tmux:

我们先来理解下tmux的几个元素。tmux的主要元素分为三层:

  • Session 一组窗口的集合,通常用来概括同一个任务。Session可以有自己的名字便于任务之间的切换。detach和attach都是基于Session的,通过 tmux 命令创建一个Session。
  • Window 单个可见窗口,Windows有自己的编号。在tmux的session中通过 C-b+c 来创建。
  • Pane 窗格,被划分成小块的窗口。通过在Window中 C-b+" (水平分割,左右两个Pane) 或者 C-b+% (垂直分割,上下两个Pane)。

即:
session: 会话,一个服务器可以包含多个session
window: 窗口,一个session可以包含多个window
pane: 面板,一个window可以包含多个pane

快捷键一览:

tmux    #开启tmux,新建一个session: t
tmux ls #显示已有tmux列表(C-b s)
tmux attach-session -t n #重新连接上第n个session, 即attach。或者: tmux a –t n
C-b c #创建一个新的窗口
C-b n #切换到下一个窗口
C-b p #切换到上一个窗口
C-b l #最后一个窗口,和上一个窗口的概念不一样哟,谁试谁知道
c-b w #通过上下键选择当前窗口中打开的会话
C-b n #直接跳到第n个窗口
C-b & #退出当前窗口(kill window,同时关闭所有小窗口)
C-b x #关闭当前光标处的小窗口或者如果只有一个窗口则关闭window

C-b d 临时断开会话,即dettach,类似screen的 ctrl+a+d (通过tmux ls查看Session号,例如0,然后tmux attach-session -t 0 重新attach)
C-b " 分割出来一个窗口
C-b % 分割出来一个窗口
C-b o 在小窗口中切换
C-b (方向键)
C-b ! 新建一个窗口,并将当前小窗口最大化

版权声明:本文为博主原创文章,未经博主允许不得转载。

screen & tmux 技巧:linux后台运行程序

标签:screen   commands   logging   后台   

原文地址:http://blog.csdn.net/gw569453350game/article/details/46530367

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!