Screen & tmux tips: linux background programs
General introduction:
Simple screen application:
Screen myprogram // start a program through screen, or screen-L myprogram: turns on logging ~ /Screenlogtailctr + a and then press d // to run the program in the background, that is, detachscreen-ls // list the programs running in the screen background, and the IDS (such as 4491. pts-2.localhost) screen-r ID // resume the program to terminal, for example: screen-r 4491 exit // exit the current screen session (same as ctrl + a and then press k ), sometimes many screens are opened, and you can exit through exit.
Multi-Window Management:
If a screen is run multiple times on a terminal, that is, a nested multi-layer screen, press ctrl + a to view all the screen Windows under the current terminal, * Indicates the window where the current session is located, and press ctrl + a to 0 ~ 9 to switch to a corresponding window.
A good article about screen, just read it:
Http://www.ibm.com/developerworks/cn/linux/l-cn-screen/
The Application of Complex points is as follows:
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:
Let's first understand several elements of tmux. The main elements of tmux are divided into three layers:
A set of windows in a Session group, which is usually used to summarize the same task. Sessions can have their own names to facilitate switching between tasks. Both detach and attach are based on sessions. You can use the tmux command to create a Session. Windows: A single visible Window. Windows has its own number. In the tmux session, C-B + c is used for creation. Pane, divided into small window. In the Window, C-B + "(horizontal split, left and right Pane) or C-B + % (Vertical Split, up and down two Pane ).
That is:
Session: session. A server can contain multiple sessions.
Window: window. A session can contain multiple windows.
Pane: panel. A window can contain multiple pane.
Shortcut Keys:
Tmux # enable tmux and create a session: ttmux ls # display the list of existing tmux (C-B s) tmux attach-session-t n # reconnect the first n sessions, that is, attach. Or: tmux a-t nC-B c # create a new window C-B n # Switch to the next window C-B p # Switch to the last window C-B l, unlike the previous window, who knows c-B w # Use the up/down key to select the Session C-B n in the current window # directly jump to the n Window C-B & # exit the current window (kill window, close all small windows at the same time) C-B x # Close the small window at the current cursor or close the window if there is only one window.
C-B dTemporarily disconnect the Session, that is, dettach, similar to screen ctrl + a + d (view the session number through tmux ls, for example, 0, and then tmux attach-Session-t 0 re-attach)
C-B "split a window
C-B % split a window
C-B o switches in a small window
C-B (direction key)
C-B! Create a new window and maximize the current small window