screen&tmux技巧:linux後台運行程式

來源:互聯網
上載者:User

screen&tmux技巧:linux後台運行程式
通俗簡介:

screen 簡單應用:

screen myprogram     // 通過screen啟動一個程式, 或者screen -L myprogram: turns on logging to ~/screenlog.0ctr+a 然後再按一下 d   // 使程式在後台運行,即detachscreen -ls           // 列出在screen後台啟動並執行程式,以及ID(例如4491.pts-2.localhost)screen -r ID         // 恢複運行程式到terminal,例如:screen -r 4491exit //退出當前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 -lsThere 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: ttmux ls #顯示已有tmux列表(C-b s)tmux attach-session -t n #重新串連上第n個session, 即attach。或者: tmux a –t nC-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 ! 建立一個視窗,並將當前小視窗最大化

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.