Monday, August 8, 2011

Basics of screen command in linux

When you working on remote machine, you may come across with the situation, like, run a program or script for couple of hour or days. But when you logout of your local machine then your program automatically get killed because of loss of your session(Assigned shell gets terminated). So linux provides you to use "screen" command which allows you to make your program independent of shell. So even when you logout remotely even your program runs. Here the steps follows.
Log in to remote machine.
First check out "screen" is available in your remote machine.
If not present, use "apt-get install screen" cmd in ubuntu and install.
After successful installation.
Start ur program using below command.
Now ur program running in foreground only.
Now send detach signal by sending "Ctrl+a d" signal. Now your process will detached from your shell and running in background.
Now can logout from remote machine, process will run in remote machine without any effect.
Now after some time or even after some days, log in to remote machine and run "screen -ls". It will display all process running by using screen.
For attaching your process to shell run "screen -r ". Now your process will come foreground.
If multiple process are running using screens then "screen -r " below given example.
[ ~]#screen -ls
There are screens on:
29383.pts-7.localhost (Detached)
31899.pts-9.localhost (Detached)
2 Sockets in /var/run/screen/S-root.
These 29383, 31899 are pids of screens so
[ ~]# ps -ef |grep SCR
root 10787 15582 0 14:43 pts/9 00:00:00 grep SCR
root 29383 1 0 14:21 ? 00:00:00 SCREEN process1
root 31899 1 0 14:39 ? 00:00:00 SCREEN process2
To bring process to foreground
[ ~]#screen -r 29383
Now SCREEN process1 process comes to foreground.
So what are you looking for, give a try....

No comments: