Pythian Blog: Technical Track

Easy Transition from Screen to tmux

 

Do you use Screen in Linux?

If so, I’m sure you appreciate the importance of functionality it provides.

At times, as Oracle E-Business Suite database administrators, we need to run long running processes (like fs_clone) in the background and Screen provides an option to attach to a session window running in the background and see what is going on with the process.

Screen is being deprecated in RHEL8 and the replacement is tmux.

Tmux provides much the same functionalities, with some additions obviously.

While there are many articles out there explaining all the fine details of both utilities, in this article, I intend to showcase an easy way of getting started with Tmux and a side by side command comparison with Screen which you can use in your day to day work life.

 

Creating a Named session

More often than not, you would be interested in running more than one process in the background.

To easily identify which session is running what process, it is a good idea to create a named session. Here is how you do it:

For screen
screen -SL SCREEN_TEST

This command creates a new named screen session called SCREEN_TEST and also creates a log file named screenlog.0

For tmux
tmux new -s TMUX_TEST

This command creates a new named tmux session called TMUX_TEST and does not create a log file automatically unless the logging option is explicitly configured in the tmux configuration file.

Listing all running screen/tmux sessions

For screen
screen -ls
For tmux
tmux ls

Attaching to an existing named session

For screen
screen -r SCREEN_TEST
For tmux
tmux attach -t TMUX_TEST

How to tell you are in a screen/tmux session:

For screen

Run “echo $STY” in screen session to show the session details. Please refer to the screenshot below:

For tmux

Use the bar at the bottom of tmux session. Every tmux session, unless the default options are altered, has a bar at the bottom of the screen which shows details of the tmux session like the name of the session, window index, connection information, hostname, time, etc. Please refer to the screenshot below:

Detaching from a named session

 

For screen

For accessing the command mode in screen use : “Ctrl+a” , anything typed afterward will denote the command you intend to run

Ctrl+a d
For tmux

For accessing the command mode in tmux use : “Ctrl+b” , anything typed afterward will denote the command you intend to run

Ctrl+b d

Terminating screen/tmux sessions

 

For screen

Attach to the sessions and run:

  • Ctrl+a, k : this will ask for confirmation
  • exit: this will just exit the session

For tmux

Attach to a tmux session and run: 

  • Ctrl+b, x : this will ask for confirmation
  • exit: this will just exit the session

Share a screen session between two different SSH connections so two people can see the same thing, in real-time

 

For screen

Run the following command against an already attached session:

screen -x SCREEN_TEST

For tmux

Just attach to the tmux session

tmux attach -t TMUX_TEST

Concept of session, window, and pane

For both screen & tmux

One session can have multiple windows and one window can have more than one pane.

How to create new windows in screen/tmux session

For screen

Attach to an existing screen session and run the following command:

Ctrl+a c

For tmux

Attach to an existing tmux session and run the following command:

Ctrl+b c

How to list all windows in a screen/tmux session:

For screen

Attach to an existing screen session and run the following command:

Ctrl+a "

For tmux

Attach to an existing tmux session and run the following command:

Ctrl+b w

How to cycle between windows

For screen

Attach to an existing screen session and run the following command:

  • Ctrl+a 0 or Ctrl+a 1 
  • Ctrl+a n/p for next or previous screen window

For tmux

Attach to an existing tmux session and run the following command:

  • Ctrl+b 0 or Ctrl+b 1 
  • Ctrl+b n/p for next or previous tmux window

Split your terminal window into vertical or horizontal regions

For screen

Attach to an existing screen session and run the following command:

    Ctrl+a, | (pipe)            : Split the window Vertically. 
    Ctrl+a, (Caps) S.           : Split the window horizontally. 
    Ctrl+a, Tab                 : Move to the lower region. 
    Ctrl+a, 0                   : Display window zero in the lower region. 
    Ctrl+a, "                   : Display a list of all the windows 
    Ctrl+a, (Caps) X            : Close the current region.
For tmux

Attach to an existing tmux session and run the following command:

    Ctrl+b, %                 : Split the current pane vertically, left and right.
    Ctrl+b, Up,Down Arrow     : To move from one pane to another, press 
    Ctrl+b, "                 : Split the window horizontally into two panes.
    Ctrl+b, q                 : Make tmux briefly flash the number of each pane.
    Ctrl+b, x                 : Close the current pane

 

How to configure logging in screen/tmux

 

For screen

Create a file $HOME/.screenrc with the following entries. The options can be adjusted according to your needs by updating the .screenrc file.

logfile screenlog.%S.%Y.%m.%d.%c.%s.log
logfile flush 1
log on
logtstamp after 1
logtstamp string "[ %t: %Y-%m-%d %c:%s ]\012"
logtstamp off
startup_message off
hardstatus alwayslastline "%{= bR}[%{= bY}%H%{= bR}]%{= bY} %n%{= bR}:%{= bY}%t  %{= bw}%W %=%{= br}[%{= bY}%h%{= bR}]"

 

For tmux

Create a file $HOME/.tmux.conf with the following entries, this will create all log files in $HOME/tmux_logs directory. The options can be adjusted according to your needs by updating the .tmux.conf file

set-option -g default-command 'tmux pipe-pane -o "cat >>~/tmux_logs/tmux-#{session_name}-#{window_index}-#{pane_index}-`date +%Y%m%dT%H%M%S`.log" ; /bin/bash -l'

Every time a new tmux session is created, this option will create a log file with session name, window index, pane index, and timestamp.

There may be other and potentially better ways of creating log files, but I find this option handy as it creates a tmux session log file with all the relevant information in its name.

Hope this comparison has provided an easy way to transition from screen to tmux. Make sure to sign up for the next post.

 

No Comments Yet

Let us know what you think

Subscribe by email