Learning Linux: Using Screen to Keep Processes Running
Next up on the Learning Linux series, I will talk through a helpful tool called Screen. Screen is a powerful tool that allows you to open many terminal instances from a single terminal instance. Screen’s man page describes it as:
“...a full-screen window manager that multiplexes a physical terminal between several processes (typically interactive shells). Each virtual terminal provides the functions of a ... terminal and, in addition, several control functions...”
One way this command is used is to keep a process running after closing an SSH connection to a server. If you SSH into a server, start a process, then close the SSH connection, the process will be terminated as well. Screen will allow you to keep a process running, giving you the ability to safely close the SSH section.
For example, if you need to run a long process, such as a long recursive copy or a large
dd operation, you would first connect to the server via SSH, then enter
screen -S [name], so you can start the Screen session and name it for easy identification. You don’t need to use the -S option and a name, but it’s a good practice if you have more than one Screen session.
After that, you will now be in the Screen session, and you can run any commands as normal. Any long-running processes can now begin. Sometimes, you’ll want to run more than one related process at the same time. Screen can also accommodate that by using “windows.” A Screen session can have many windows within, and you can alternate between them once you are in that Screen session. To create a new window in Screen, you would type
Ctrl+a, c. When you type
Ctrl+a in Screen, this sends a signal to send the commands to Screen instead of the shell. In the new window, you can start another process while the first keeps going. That way, related processes remain in the same Screen session on different windows, instead of on separate Screens altogether. To alternate between windows, you use
Ctrl+a, n. You can also list all windows in a session and choose one by typing
Once you have your Screen processes running, you use
Ctrl+a, d to get back to the initial terminal instance, detaching the Screen session. You can then close the SSH connection if needed and the Screen will remain running. If you need to check on a Screen process, you would SSH back in, and if you have only one Screen session running, simply type
screen -r to reattach the Screen. If you have more than one Screen session, type
screen -ls to see them all. This is where naming Screen sessions is helpful, since it helps easily determine which Screen to reattach. The Screen list will show a PID in front of each screen session. You can use that to reattach a specific Screen using
screen -r [PID], or use the Screen session name:
screen -r [name]. You can also use the full Screen name listed, but brevity is generally best in terminal.
Once done, you can end the Screen process. One is within Screen itself by using
Ctrl+a, k. This kills a window, and if there is only one window, it will kill that Screen session as well. Outside of screen, you can use
screen -ls to get the Screen details, and use this to quit it:
screen -X -S [PID/name] quit (as it appears, you can use either the PID or Screen name). Since you also have the PID, you can use the
kill command as well.
That’s just a brief overview of using Screen. There are many options that can be used to fine tune your use of Screen, and as always, man pages will tell you more. Stay tuned next time for more Linux commands and tips!
You can check out some of my previous Learning Linux posts below.