Naming Screen Sessions

I develop a number of Django-powered websites at work, and usually I want to leave them running when I’m not working on them so others can check out my progress and give me suggestions. The Django development server is incredibly useful when developing, but it’s not detached from the terminal so as soon as you log out the server gets switched off. One alternative is to run the website under Apache, as you would deploy it normally. This solves the problem of leaving the website running, but makes it much harder to develop with.

A third option is the GNU program Screen. When run without arguments screen puts you into a new bash session. Pressing Ctrl+d drops you back out to where you were. The magic occurs when you press Ctrl+a d. This drops you back out, but the bash session is stilling running! By typing screen -r you’ll reattach to the session and can carry on working as before. You can leave it as long as you like between detaching and reattaching to a session, as long as the computer is still running.

It is possible to run multiple screen sessions at once, perhaps with a different Django development server running in each. Unfortunately screen will only reattach automatically when there is just one detached session. If you have more than one then you’ll be confronted by a cryptic series of numbers that uniquely identifies each session. You can reattach to a specific session you can type screen -r <pid>.n To make things easier to reattach to the session that I’m working on I give these sessions name so rather than a cryptic series of numbers I see a useful set of names. To do this you just need to type Ctrl + A : sessionname name.

There are plenty of other useful things that screen can do, but named sessions is by far and away the most common one that I use.

Want to read more like this? Follow me with your favourite feed reader (e.g. Feedly), or subscribe to my SubStack newsletter.

Comments

You could also just use one screen session and create a new window inside your session with "^a c". Than you can switch between windows with "^a n" and "^a p" (next and previous). You can also name windows and let screen display these names in a "tab bar". Just google up "screen hardstatus"

bjunix

03 Feb 2010

It's also possible to name the screen when you create it, like so:

screen -S nameofscreen

Then when you want to attach to it later

screen -r nameofscreen

Also useful: multiple people can attach to the same screen with -x in place of -r.

P. F. Hawkins

03 Feb 2010

Screen is nice, I usually just use nohup.
nohup python manage.py runserver &

Then everything is logged to nohup.log

Justin

03 Feb 2010

./manage.py runserver &

done!

juanpex

03 Feb 2010

Stuff started with `stuff &` runs in the background but is still attached to your session. When you terminate your session (eg, logout), it will terminate any background processes.

Chris Webstar

19 Jun 2010