Screen is one useful tool that can be used to run processes in background. Usually this utility is not shipped with CentOS distributions but, you can easily install it anytime. Linux OS also has inbuilt features to run background processes but, I personally find screen much easier and more elegant rather than using (&, fg, bg, jobs).
First make sure that screen is installed, you can verify if the package exists by running: rpm -qa | grep screen.
If the software is not installed, run yum install screen:
To open a new session simply type screen. You can also specify a session name by running screen -S session_name. A new terminal will be opened in which you can execute whatever you want. For this exercise execute top:
Screen can be now be manipulated using Ctrl+a and a letter or symbol. For example, you can execute Ctrl+a then ? to view the help screen:
To return to the shell without closing the current top execution type Ctrl+a d. This command will detach but the shell window will still be opened.
Ctrl+a c is used to create a new window
Ctrl+a k will kill the current window
Ctrl+a w will list all screen windows
You can start another screen window and execute something else in parallel. If you type screen -r or screen -ls you’ll see a list with detached sessions:
To enable a detached session execute screen -R session_name or screen -R session_pid command.Because we’ve created a session named ‘ppscu’, I’ll execute screen -R ppscu. Note that if only one session is in detached state, screen -R command will enable that session automatically.
If a screen session is attached to another terminal you can use the screen -d -R session_name or screen -d -R session_pid to attach it to the current terminal. This command will first detach it from the old terminal before attaching it to the current one. For this example I’ve used screen -d -R 4438.ppscu because I’ve opened multiple screen windows within the ppscu terminal:
These are the main operations that you can use with the screen command. Note that you ca further discover its features by looking into its man page (man screen command).