520

I'm using the Screen multiplexer tool on the command shell and open a lot of screens. I then forget which process ID associates with which task.

I would like to set a name for a screen, but I can't find an option in the man page.

Currently, listing the screens looks like this:

There are screens on:
    5422.pts-1.aws1 (Detached)
    5448.pts-1.aws1 (Detached)
    5027.pts-1.aws1 (Detached)
3 Sockets in /var/run/screen/S-sb.

And I would like to see something like this:

There are screens on:
    5422.logCleanWorker (Detached)
    5448.overNightLongTask(Detached)
    5027.databaseOverNightLongTask (Detached)
3 Sockets in /var/run/screen/S-sb.

How can I do this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
spankmaster79
  • 21,555
  • 10
  • 42
  • 73
  • 6
    the question looks more like it's asking "how to rename existing sessions" rather than "how to start new sessions with a name". In that context, the currently chosen answer is not really appropriate. The correct answer should rather be the one given by `user164176` - http://stackoverflow.com/a/3309696/636762 – Rakib Jun 12 '16 at 16:05
  • The names of your screen sessions suggest that you create a new screen session per a single task (maybe a single command). Screen supports [multiple windows](https://www.rackaid.com/blog/linux-screen-tutorial-and-how-to/#creating), so you can run all these tasks in a single screen session. – Melebius Nov 30 '17 at 07:51
  • 3
    Can this be migrated to SuperUser (or some other StackExchange site)? Obviously it's quite useful to a lot of people, though it's out of the scope of this particular site. – Patrick Sanan Apr 19 '18 at 06:04
  • 1
    if you find an option on this site to move it there with all the history, then I would agree moving it – spankmaster79 Apr 23 '18 at 11:51

5 Answers5

769

To start a new session

screen -S your_session_name

To rename an existing session

Ctrl+a, : sessionname YOUR_SESSION_NAME Enter

You must be inside the session

Community
  • 1
  • 1
user164176
  • 7,714
  • 1
  • 16
  • 2
  • 21
    This is the appropriate solution as it focuses on renaming the existing session rather than creating a new one. – thanos.a May 08 '15 at 07:36
  • 5
    A quick note on `C-a :` syntax.... "All screen commands are prefixed by an escape key, by default `C-a` (that's Control-a, sometimes written `^a`). To send a literal `C-a` to the programs in screen, use `C-a a`. This is useful when working with screen within screen. For example `C-a a n` will move screen to a new window on the screen within screen." (ref: http://aperiodic.net/screen/quick_reference ) – Nate Ritter Jul 08 '16 at 16:12
  • Much appreciated. Even though I know you can start with a name, I often start without naming, then wish I could later. Thanks! – Ketzak Jul 31 '17 at 18:40
  • 2
    agreed after looking at it a couple of years later :D – spankmaster79 Apr 17 '18 at 09:52
  • Can I bind the `C-a :sessionname NEWNAME` to something quicker to type? I tried `bind $ sessionname` but now when I `C-a $` it doesn't prompt me for a new name. Is that somehow possible? – andreas-h Feb 10 '19 at 12:16
  • 1
    Pro tip, you can type a prefix of the screenname, so instead of: `screen -r myname`, `screen -r my` will work (if it's a unique prefix). – gub May 16 '19 at 20:49
602

To create a new screen with the name foo, use

screen -S foo

Then to reattach it, run

screen -r foo  # or use -x, as in
screen -x foo  # for "Multi display mode" (see the man page)
Sophie Alpert
  • 139,698
  • 36
  • 220
  • 238
miedwar
  • 7,248
  • 1
  • 16
  • 15
  • 18
    In you solution you explain how to create new ones with correct names rather than changing the existing ones. There is none that wants to terminate the session if it is possible to keep it. – thanos.a May 08 '15 at 07:36
  • 11
    Scroll down to user164176's answer to see how to rename existing sessions. – jlh Jan 27 '16 at 10:44
  • 1
    the question looks more like it's asking "how to rename existing sessions" rather than "how to start new sessions with a name". In that context, the currently chosen answer is not really appropriate. The chosen correct answer should rather be the one given by `user164176` - http://stackoverflow.com/a/3309696/636762 below – Rakib Jun 12 '16 at 16:06
  • I searched "ubuntu create a new screen with name" on google and this was first result. Exactly what I was looking for :) thanks – Skylord123 Mar 22 '17 at 03:09
  • 1
    To rename a session: Ctrl+a, : sessionname YOUR_SESSION_NAME Enter. – Slake Apr 13 '18 at 13:34
54

As already stated, screen -S SESSIONTITLE works for starting a session with a title (SESSIONTITLE), but if you start a session and later decide to change its title. This can be accomplished by using the default key bindings:

Ctrl+a, A

Which prompts:

Set windows title to:SESSIONTITLE

Change SESSIONTITLE by backspacing and typing in the desired title. To confirm the name change and list all titles.

Ctrl+a, "

techraf
  • 64,883
  • 27
  • 193
  • 198
E4Y
  • 701
  • 5
  • 2
  • 44
    You are treating the terms _session_ and _window_ as interchangeable when they are not. `Ctrl+a`, `A` does not set the _session_ name (`SESSIONNAME`), it only sets the current _window_ title. Titles of windows within a session do not appear in the list of sessions when queried with `screen -list`. The question asks how to set the _session_ name. – StvnW May 14 '14 at 12:59
  • 6
    usefull but it changes current window title, not session name. – Znik Jan 09 '15 at 14:25
17

The easiest way is to use Screen with a name:

screen -S 'name' 'application'
  • Ctrl + a, d = exit and leave the application open

Return to Screen:

screen -r 'name'

For example, using Lynx with Screen.

Create a screen:

screen -S lynx lynx

Ctrl+a, d = exit

Later, you can return with:

screen -r lynx
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
OneOFPunx
  • 293
  • 2
  • 2
  • 21
    Three problems with this. First, several people have already posted this. Second, you're telling him basic screen usage stuff he already knows. Third, you didn't actually answer the question, since he asked how to name an already-existing screen session. – Steely Dan Oct 31 '12 at 13:41
0

I am a beginner to Screen, but I find it immensely useful while restoring lost connections.

Your question has already been answered, but this information might serve as an add on - I use PuTTY with PuTTY connection manager and name my screens - "tab1", "tab2", etc. - as for me the overall picture of the 8-10 tabs is more important than each individual tab name. I use the 8th tab for connecting to db, the 7th for viewing logs, etc. So when I want to reattach my screens I have written a simple wrapper which says:

#!/bin/bash
screen -d -r tab$1

where first argument is the tab number.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Saurabh Hirani
  • 1,198
  • 14
  • 21