PDA

View Full Version : Linux command "screen" - keep commands running; work in separate window



Fli
12-09-2013, 01:35 PM
Do You need to run some process in shell/linux command line/terminal/SSH and be able to exit your SSH terminal session and restore that session anytime later? Or just run some automated task and keep working in temrinal?

The "screen" command can be used for this purpose.

"Screen" Installation / Veriffy if installed

Look if screen app is found on your linux:

which screen

a) if nothing found install it at rhel (
yum install screen), at debian (
apt-get install screen)
b) if screen is found/is installed, then we can proceed further:

===============
Quick scheme of the "screen" command
screen - will create new window
...now run some task you want to run at background...
"Ctrl+a" and "d" (or screen -d) - detach the window
...now exit ssh or work...
screen -r - restore detached window
discover if your long task completed
"Ctrl+a" and "K" (or command: exit) -- to kill the window (or detach it again)
===============

Run commands in an Screen Window

Example: You want to download some big file or do other long automated task on foreground, but at same time you want to be able to continue working on command line or you want to be able to exit terminal and login anytime later.
So you can type command:


screen

this will create virtual terminal window which is capable to run commands, apps in it untill you terminate this window (it is a linux process)

now once you typed "screen" command and you are in the window, type the command you want to run, like big file download: wget -O /dev/null ftp://speedtest:[email protected]/test10Gb.db
once download is in progress, you want to DETACH this screen window:

Ctrl+a and then "d" (d as "detach" or deactivate)

Now you should see "[detached]" and you can continue working while your download is running in the detached window or you can exit your SSH session.

After you login again or want to restore your detached Screen Window, do command:

screen -r (r as "Reattach" or reactivate or restore)

if you have more screen windows, it will ask you to add window ID into the command. Example:

screen -r 1234

Now you restored your window and see the download in progress or finished or any other process you ran in your window.

if process not yet finished, you can detach the window again by Ctrl+a and then "d" or when process finished, you can kill the window by command:

Ctrl+a and "K" (upper case K).

If you created too much windows and you want to kill them all, you can do command: pkill screen