Working on a server via SSH
in-silico toxicology gmbh blog
Working on a remote machine is a common task, when, for example, data mining or other long running jobs are put on a dedicated machine. Having some useful tools at hand is important, because SSH connections often close unexpectedly, for example on unstable WLAN connections.
Cool Shell Tools Configurations
Your shell should be configured such that it maximizes the amount of information you can get from a text-based representation. This includes color support and other sensible settings for the most important shell tools. A dedicated repository offers an installer for some important shell tool’s configuration files.
git checkout git://github.com/amaunz/home-bin
cd home-bin
./_init.sh
This will copy configuration files for different shell tools to your $HOME
directory, including configuration for bash
, vim
, irb
, git
, screen
,
htop
, and more. These configurations are designed to maximize productivity,
customizing color support, syntax highlighting, command history, layout, among
others. Explaining all options is beyond this document’s scope. See the
configuration files for comments documenting the options.
Note: Your current settings are not overwritten. If, for any configuration
file, a different configuration is already present, _init.sh
opens a
vimdiff
(side-by-side editing) session, such that you can push and pull
changes manually to your settings.
See this tutorial to learn how to work with vimdiff.
Using htop
htop
is an ncurses-based tool for monitoring CPU, RAM, load, and processes.
See this tutorial.
Using GNU screen
Do not open several SSH sessions to accomplish several tasks. Any session creates load, while the server could use it’s CPU cycles and memory for other jobs, such as running your applications. Use a single SSH session only. It is less likely to be interrupted than many sessions at once.
Also use the “screen” utility. It even saves your work when your single SSH session is interrupted. This is how a screen session can look like.
It shows two regions (a larger and a smaller one) on a single window. The status bar at the bottom includes hostname, windows list (currently 4), and CPU load (part of the cool shell tool configuration package).
Start screen session:
screen -S "screentitle"
This puts you inside a session called “screentitle”. Start only one session, then use as many windows as you want inside this screen session.
Working with several windows inside screen:
You can have several so-called windows inside a screen session, which you can imagine as tabs or sheets. You can switch between them. Every window inside a session is a full terminal. You can run different things on each window, just as you did previously with several SSH connections, but you need only one. If the single SSH connection fails, nothing is lost. Simply reconnect and re-attach to your screen session, then go to the right window.
Ctrl+a c
Create new windowCtrl+a k
Kill the current window / sessionCtrl+a w
List all windowsCtrl+a 0-9
Go to a window numbered 0 9, useCtrl+a w
to see numberCtrl+a Ctrl+a
Toggle / switch between the current and previous windowCtrl+a :fit
Fit screen size to new terminal size. You can also hitCtrl+a F
for the the same taskCtrl+a D
Power detach and logout. Think twice before doing this!Ctrl+a d
Detach but keep shell window open. This is safe!Ctrl-a ?
Display help screen i.e. display a list of commands
You can also have so-called regions that split your screen window, so you can monitor different things at the same time.
Ctrl+a S
Split terminal horizontally into regions and pressCtrl+a
c to create new window thereCtrl+a :resize
Resize regionCtrl+a tab
Move to next regionCtrl+a :remove
Remove / delete region. You can also hitCtrl+a X
for the same taks
Scrolling
Ctrl+a esc
Start scroll mode. Scroll up and down using the arrow or page up/down keys. Hitesc
again to leave scoll mode.
Leave screen session (but keep it running in the background):
Inside screen, press Ctrl+a d
. This detaches the screen session and runs it in the background. The same happens if your connection is interrupted.
Re-attach to screen session:
screen -ls # show running session names as PID.screentitle
screen -r PID.screentitle # re-attach to sesssion
You should be back to your session named “screentitle”.
Try it: open a screen session on a remote machine, then pull the network cable.
blog comments powered by Disqus