Previous to one of my work colleagues of more years experience in working with LINUX and UNIX enlighten me to this most useful command I believe most of you out there like me would fall into four camps:
- Remote desktop machine probably windows that you log into and it stays up with you command running that you can come back to later
- Sit around with your command window on your machine, waiting for it to be finished as the rest of your colleagues go home
- nohup a script, but once you have closed your session you don’t know when your script has finished
- CRON tab a script
There is a fifth way screen
Screen your new best friend
So once you have ssh’ed into your server and got to the command line you can use screen too create a server based terminal session that can be attached to and detached at you leisure. Screen works on “sessions” for the user you are logged in as and these sessions can be connected to and disconnected from as that user, for the life time of the session. A full list of options to the screen command can be found here.
Create a session with screen
You have your command line type in screen and press enter:
You will be greeted with this message and prompted to press enter or space and you will be taken to a command line again, you are now in your screen session.
Seeing your screen sessions
You can see the screen sessions that you have available to you by typing in:
$ screen -ls
As you can see its a lot like the ls of the normal LINUX file system and you will get something back like this:
bash-3.2$ screen -ls There are screens on: 1747.ttys000.machineid (Detached) 1872.ttys000.machineid (Attached) 2 Sockets in /var/folders/XZ/XZGRJK7vHZuH1b726e+9yE++-GI/-Tmp-/.screen.
As you can see there are two screen sessions on this machine and you can only attach once to a session. Someone else must be “in” the screen “1872.ttys000.machineid”
Attaching to a screen session
So once you have found your screen sessions you can attach to a screen by using the command:
bash-3.2$ screen -r
To attach to the session above that can be used the command would be:
bash-3.2$ screen -r 1747.ttys000.machineid
This will now allow you to run commands and leave commands on screen as you log on and off. If you try to connect to a session that no longer exists you will get this:
philipcarrington$ screen -r 1747.ttys000.machineid There is no screen to be resumed matching 1747.ttys000.machineid.
Disconnecting from a screen session
Here you have to be slightly careful in that if you press the wrong keys then you will exit your session and you will lose what it is doing, but depending on what it is doing it will not stop potentially. The command exit will work as normal as it you were in a terminal and return you to the “main” terminal session on the server:
Return to the main session with
$ screen -r 1747.ttys000.machineid [screen is terminating]
If you wish your session to continue so that you can reconnect and your command will continue running until it terminates you need to press ctrl-a followed by ctrl-d. When you do this you will be returned to the terminal that spawned the session with something like:
$ screen [detached]
You can then list the available sessions and see this one and then attach again later.
Another way to disconnect a session and end it is to just use ctrl-d and you will see something similar to using exit:
$ screen -r 9679.ttys000.machineid [screen is terminating]
This is a light look at the command and as stated before you can find the full commands options here. As you can see this command is very useful and can potentially reduce your costs as you don’t need a remote desktop machine. I use this command quite extensivly especially when your connection to your server is not guaranteed for a long time like over a company VPN.