Using GNU SCREEN when working in Terminal
What is GNU SCREEN
GNU SCREEN (screen) is a text-based program usually described as a window manager or terminal multiplexer. While it does a great many things, its two biggest features are its detachability and its multiplexing. The detachability means that you can run programs from within screen, detach and log out, then log in later, reattach, and the programs will still be there. The multiplexing means that you can have multiple programs running within a single screen session, each within its own window. You can display multiple windows at once, or just switch between them, even if you only have a single terminal window to use.
Benefits of using screen
If your local computer crashes or you lose the connection, the processes or login sessions you establish through screen don’t go away. The screen program creates multiple processes instead of multiple Unix login sessions, which means that it is resource-efficient. Using the detach feature, you can save screen processes when logging out and resume where you left off, saving the trouble of restarting them.
Why not just use ‘&’ or CronTab?
Running a MySQL query with ‘&’ alone does not guarantee your query will not be interrupted. You will have to use
nohup (ignore the hangup signal). From our experience, this did not always work. Screen provides an elegant and user-friendly way to run multiple queries without having to fear it being interrupted or not being able to return to it at any time.
CronTab does not let us specify a year. This means if you forget to remove an entry it will run the next year again, automatically, even if you are not with us any longer! It also does not allow us to kick it off manually.
When should I use screen
We would recommend you always you screen when working in PuTTY (Terminal). Have a screen dedicated as your sandbox environment and create additional screens as required for long running queries or any other use case you might encounter.
NOTE: Do not use background processes (&). When you detach from your screen it will run in the background and you can start a new screen. dsd
How to use screen
- SSH into your server as you normally would.
screen -dR "your username"to create a named screen for you to work in. If you type
screen -lsyou will now see you have a socket like this:
PL-MS-i5-2019# screen -ls There is a screen on: 45.pieter (02/08/19 14:01:16) (Attached) 1 Socket in /run/screen/S-root.
Here I can see I have a Socket named
45.pieterand I am currently connected to it since it is Attached.
Now you can run your commands as you normally would. For Example:
mysql my_test_script.sql > output.log 2> err.log
To detach from a session and let it run in the background press
Ctrl+athis tells screen you will be inputting a command and then press
dto detach from this session. You should see a message like:
[detached from 45.pieter]You are now free to close your terminal. You will see this socket is now detached when you run
PL-MS-i5-2019# screen -ls There is a screen on: 137.pieter (02/08/19 15:24:35) (Detached) 1 Socket in /run/screen/S-root.
To get your screen back you need to reattach to it with
screen -dR pieterif you need to connect to a specific one.
You can close and terminate any screen you are attached to and working in at any time by pressing
Ctrl+a and then typing
:quit. A short way is simply typing
exit but you do carry the risk of exiting PuTTY completely.
If you close your ssh connection without detaching or lose your network connection: run
screen -dR <screen name> The -dR means “find my screen session, detach it from wherever it’s attached if it’s attached, and reattach it here, if I don’t already have a session, create one”
Recommended Usage Guidelines
- Use descriptive screen names. This will help you keep track of what they are for. For example:
- Use multiple SCREENs to separate your big loads from quick ad-hoc work and investigations.
- All expected long running queries should be done in SCREEN to prevent interruptions or delays.
- Don’t use nohup or &. Instead of running multiple queries with &, you should now create multiple screens for each query.
- You can still open multiple terminals and you will be able to see your screens across them.
2>command line operators let you write your script results to a file (>) and write an error log (2>). For example:
mysql my_test_script.sql > output.txt 2> err.logor
mysql -e "SELECT * FROM tableName" > output.log 2> err.log
- Do good housekeeping by exiting (
Ctrl+a :quit) your screen when you are no longer going to use it.
- You can use split windows within SCREEN if you feel it will make you more productive. It works well if you are connecting to various servers and running commands. It is less useful to run multiple SQL queries and can sometimes cause issues which will require a force quit.
|Start New Screen
|screen -dR “your username/screen name”
|Create New Window
|List your screens
|exit / Ctrl+a :quit
|screen -x or screen -x PID
|Move Between Windows
To force quit a screen session:
screen -S <sessionname> -p 0 -X quit
For a full Quick Reference list see: SCREEN Quick Reference