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

时间:2023-01-11 14:35:47

通俗简介:

screen 简单应用:

screen myprogram     // 通过screen启动一个程序, 或者screen -L myprogram 加上 -L 是为了 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中滚屏:

Ctrl+a+[ to enter Copy Mode, then scroll up/down using keys j or k.

多窗口管理:

如果在某一个终端中运行了多次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
C-b " #水平分割窗口(上下两个小窗口)
C-b % #垂直分割窗口(左右两个小窗口)
C-b (方向键) # 在小窗口中切换
C-b(不放)+左右方向键 # 调整小窗口的大小,或者上下方向键调整上下窗口大小
C-b x #关闭当前光标处的小窗口或者如果只有一个窗口则关闭window
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 d 临时断开会话,即dettach,类似screen的 ctrl+a+d (通过tmux ls查看Session号,例如0,然后tmux attach-session -t 0 重新attach)

C-b o 在小窗口中切换
C-b [ 启动滚动条,可以通过上下箭头键或者鼠标滚轮来查看内容;或者对所有窗口开启的话: C-b后,输入setw -g mode-mouse on。
C-b ! 新建一个窗口,并将当前小窗口最大化

哈,家里的小工厂,占个位: http://www.herofireworks.com/