Suppose you want to run a program persistently (even if a terminal window closes, for example). For that, you might use the nohup command. But what if you want to start a command-line session on one computer and then go home and resume that session? For something like that, you’ll want to use screen. Screen is also really handy because you can have multiple shells running in one terminal window.
Here’s the basics of how to use screen on a Linux/Ubuntu machine. First, create a session:
screen -S sessionname
It’s good to choose a descriptive session name. For example, before I switched to using Gmail I used mutt. So I would often start a session with the command “screen -S mutt” in one terminal window. If I went home, I could attach to that session from home, so I never needed to start-up or shutdown mutt.
You can easily have 10 interactive shells (numbered from 0 to 9) open in one terminal window. When you start a new session, you’ll be in shell 0. To create a new shell, hold down the “Control” key, press and release the ‘a’ key, and then type the letter ‘c’. I’ll write this as:
To create a new interactive shell, press Control-a c
Switching between shells is easy too.
To switch between shells, press Control-a # where # is the number of the shell. For example, to switch to shell number 1, press Control-a 1
VoilΓ ! Now you can easily run 10 different shells in one terminal window! But what about if you want to go home and attach to a session from home? Use something like ssh to get on your work machine. Then here’s the command:
screen -d -R sessionname will resume a session that you started in a different location.
Technically this command is doing some special things. If you started the session somewhere else, the “-d” option will “detach” it at the other location, and “-R” will reattach your current terminal window to the session. If sessionname hasn’t been created, it will create the session for you. So the command above will migrate a session to your current window and disable the session at other locations. That’s usually what you want.
To exit a session, it’s easiest to exit all the shells in the session. If you want some more info on screen:
Control-a ? will give help inside of screen.
Typing “man screen” at a Linux command-line will show more help on screen.
There are web pages with more info on screen.
Update: If you want to know more about screen, see my post about .screenrc.
You can do many more shells (normally called “windows” in screen parlance, I believe) than just 10. Looking at my current screen session, I’ve got 12 and that’s because I closed some. Ctrl-a Ctrl-n will advance to the next window and Ctrl-a Ctrl-p will go to the previous window. Once you get to the end of the list it will wrap. Ctrl-a Ctrl-a will let you quickly switch back to the last window you had active. You can also do Ctrl-a ” (that’s double quotes) to get a list of all the windows you currently have open. Combine that with Ctrl-a A to set the title of a window and suddenly you can have a listing of what everything is (assuming you set all the titles accordingly).
Screen is an awesome program and probably the unix tool I use the most.
I’m a big fan of screen. For us emacs users, the default ctrl+A shortcut that screen uses is annoying. To change it, simply do
echo “escape ^Zz” > ~/.screenrc
This will change the screen shortcut key to ctrl+Z. This works much better for me because with screen I no longer need to put processes in to the background (via ctrl+Z).
I’ve been using screen for about 15 years now. It’s great. But I always thought I was (almost) the only one. My colleagues seem to like to have a dozen or so shell windows on the screen rather than a single window with a screen session. Of course, these windows aren’t even portable to your home computer!
As a quick tip, if you have named your screen windows (using Ctrl-a A) and want to see a list of your opened windows, use Ctrl-a w and you get the list on the status line. Use Ctrl-a d to detach the screen session from your login shell or xterm window.
On my personal colocated server, I basically only ever use 1 persistent screen session with many windows. To that end, I’ve added “screen -d -r” to my .login. Combined with ssh PKI I can just type ssh -l david dellanave.com and be logged into my persistent screen where I have IRC, mail, tools, etc. I’ve had a setup like this for close to 10 years.
I don’t know what I did before screen, but I don’t want to go back!
I remember the good old days of Slackware when you could do this with the Alt+F1-9 keys. I tried screen a few times but I’ve never been a big fan. I guess it is a good time to play again.
If you connect to your linux server via ssh and want to start a script that takes a while and would be stopped on a hang-up or DSL line drop, it is cool to backdrop the screen with ctrl-a d
means…
1> screen
1> run your script
ctrl-a d
log out
next day:
1> screen -r
and the last screen comes back to you!
powerful tool, if you work on 40+ linux servers π
Hi Matt!
Thanks for the tip – I wasn’t aware of that one. It’s very irritating when a terminal window either times out or your wireless drops out and you have to go through a rigmorale to get back to ‘where you were at’
Does it work fine with Putty as well (ie windows SSH terminal client)?
Cheers,
doc
Ah, kewl, thanks for that… Used something like that at Uni 20+ years ago but had forgotten with the luxury of xterms, etc.
Quite handy if I want to put a session on hold while I hang up my wireless connection to get off the train or whatever.
Whatever next, lessons in Effective Java from Josh? B^>
Rgds
Damon
If you add this to your ~/.screenrc
termcapinfo xterm|xterms|xs|rxvt ti@:te@
You’ll get scrolling like you’re used to (with your wheely mouse). this was the one thing that annoyed me about screen for a long time.
I use screen start CS server π
Tanner Lovelace, that never occurred to me — thanks for mentioning that trick!
dockarl, it does work well with Putty..
Thanks for the tip, Matt. As a beginner in Linux (Fedora) this’ll come in handy for me π
Add this to your screenrc:
caption always “%H %c | %-w%{=}%n-%t%{-}%+w”
And you will get a bottom bar indicating name of machine you’re on, a clock, and indicators in which shell you currently are. Use CTRL-A SHIFT-A to rename your shells. The names will then also appear in the bottom bar.
Instead of:
screen -d -R
you can also do:
screen -x
This resumes the session that you had open on your work machine without detaching anything. This way you’ll have exactly the same screen that you had open when you left work and when you get back to work you’ll immediately have the same screen that you left at home. With -x you can even use the two screens at the same time π
BTW if you use Putty you can also select ‘disable switching to alternate screen’ to re-enable scrolling with the mouse.
Thanks mate π
I’m keen to hear your experiences with Ubuntu (you were talking about writing about it a few months back prior to going away) – I was looking for a change from redhat recently and used ubuntu for a new server (‘Seattle’) I set up for the purposes of some crawling and ‘trend analysis’ I had to do on some sites prior to a recent big important job interview π
It’s was all pretty smooth sailing except for a crazy problem I had with this weird error {“β/bin/sh canβt access tty; job control mode off'”} I got on the first reboot after installation – turns out just popping a floppy (doesn’t matter if it has anything on it) in the drive for the first boot fixes the prob forever – truly weird, but lots of people seem to have the same problem as my ‘solution’ on my personal site has been quite highly trafficked and commented – comments like “dude I can’t believe that really works” and “If I were a woman I’d marry you” seem to be the general reaction.. hehe.
The thing I miss the most though is all the little fedora / redhat specific shortcuts – like “service smb status” etc.
Ark – cool tip! I found a similar tip for vim a while back π
doc
dockarl: You just need to learn the nuances a Debian based distro uses. Though, in your specific case, you can call the smbstatus command directly (just make sure your path statement includes the location of the binary – usually /usr/bin)
Ooh – Thanks Christopher – just done is now – smbstatus – that’s cool.
Do you know if there are equivalents for the other daemons as well? Like cups, network etc.. I only seem to be able to use “start, stop, reload, restart, force-reload” – there doesn’t seem to be any ‘status’ modifier.
But given that’s my only major (minor) gripe after having been a hard core red hat user for 5 years, I guess I’m pretty impressed (can’t comment on the GUI – it looks pretty but I very rarely use it).
doc
Matt,
Your mention of screen saved my hide yesterday, and allowed me to recover my Cisco DSL router from ‘brick’ status after 36 hours without the Net.
Kewl! WEBSPAM killer and all-round fixit… B^>
Rgds
Damon
Hey I am pretty annoyed with using C-a as prefix for every screen command. How can I change it to the “windows key (call it W)?” That is the key between C and M.
I know escape ^Zz changes it to C-z.
Also what if I need W-1 as the prefix?
Thank you in advance
Screen is one of those tools that has been around for decades, and one that has improved my productivity 10-fold.
Its handy for building while asleep too π
Thanks for everybody’s comments on screen it pushed me into using it today and it turned out to be a lifesaver.
Was working on a remote machine who’s connection drops frequently and had to copy over a number of large websites…. if the connection dropped while doing the transfer, unzipping or importing any of the files, life starts to get hard π
Screen was great as the connection would drop but when it came back I could re-attach to the session and it was still chugging along!
Kudos to screen!
Excellent tutorial on excellent utility (I have been using it for around 6-7 years)
BTW one can sync not only terminals on different computers, but Firefox browsers also
http://www.google.com/tools/firefox/browsersync/index.html
I can get screen to start with a command running in that screen, but when the command runs it’s course the screen terminates. How do I get the command to run but screen to remain? So I can run a command then come back later to check it’s output.
email me at with the any suggestions
screen@trashmail.net