How to use screen command to work in detached mode
Sometimes we want to work in detached mode to prevent connection loss during long running processes.
The ‘screen‘ command detach a long running process/program/shell-script from a session and is re-attached at a later time.
The process that was initiated from the screen is running and managed by the screen even when the session is detached.
We can re-attach the session at a later time, and our terminals are still there, the way we have left them.
Screen Command Usage
- To enter into screen mode use screen command.
- Optionally, if we want to name this screen use -S e.g screen -S ‘Ashu is working’
- Now do required work in screen mode.
- To detach a screen use CTRL + a + d (Press CTRL + a followed by d to detach current running screen)
- To list out running screen sesssions: screen -ls
- To attach to a running screen user screen -r <Name_Of_Detached_Screen>
- screen # to enter into screen mode
- ctrl + a + d # Detache from sreen mode but proces keep running
- screen -ls # tells all program running via screen
- screen -r 5363.pts-0.app999 # To resume process “5363.pts-0.app103” foune via screen -ls command
- When we have access to only one terminal, then we can use screen command and multiplex the single terminal into multiple, to execute several independent commands in eache screen.
- When we are working on another machine (say team environment), we can start some process from other machine using screen command and detach it when we are done. Later when we get back to our place, we can login and attach the screen back to your terminal.