Basics with GNU Screen
I’m aware of the fact that most people prefer Tmux today. I’m not one of those. :) It’s not because it’s bad or anything. I have always been using Screen—long before Tmux was a thing—it’s what I’m used to.
And yes. Tmux have more features than Screen, but worse is better as they say. I just want the absolute basics and not any additional complexity or anything like that. I also consider Screen to be a lot easier and straight forward to use.
Screen and Tmux are two programs that let you create sessions in the terminal that you can detach and then attach to from any terminal emulator and/or machine. This is handy when you want to run anything via the console and don’t want to be forced to always keep that window open.
Let’s say you work on a remote computer where you want to continue running a service or application that can’t be run as a service. If you close the terminal it will also close the application. If you were to run it via a screen you could then detach that session and it will continue to run in the background.
It’s always a good idea to run things in a screen over a remote connection anyway. If anything happens to your connection and you get disconnected, you might loose important work and/or time.
Table of Contents
* [Create a new session](#create-a-new-session) * [Create a new session and automatically run a command](#create-a-new-session-and-automatically-run-a-command) * [Create a new session without attaching to it](#create-a-new-session-without-attaching-to-it) * [Detaching a session](#detaching-a-session) * [List all sessions](#list-all-sessions) * [Resume a session](#resume-a-session) * [Create or resume a session in one command](#create-or-resume-a-session-in-one-command) * [Better titles](#better-titles)
Create a new session
Creating a new session is easy as:
$ screen -S <session name>
Create a new session and automatically run a command
You can also run a command directly when creating a new session:
$ screen -S <session name> "<command>"
This also works and might be useful for some scenarios:
$ screen -S <session name> sh -c "<command>"
Create a new session without attaching to it
Sometimes you might want to create a new session without automatically attaching to it. This is possible with the combination of the flags
$ screen -S <session name> -d -m "<command>"
Detaching a session
To detach a session tap and hold the key
Ctrl while tapping the keys
List all sessions
You can list all sessions with the flag
$ screen -ls There are screens on: 28049.keepassc (Detached) 3816.news (Attached) 2 Sockets in /tmp/screen/S-johan.
Resume a session
To resume a session:
$ screen -r <session name or PID>
Using the PID might be useful if you somehow ends up with two or more sessions with the same name.
Create or resume a session in one command
I run several things in various screens. One of those things is my password manager KeePassC. If I were to use the commands I just mentioned, it would require a lot of effort to create, resume and keeping track of any running session. Thankfully there’s two flags that solves that for us.
If we check the manual we will find the combined flags
-D -RR with the descriptive text:
-D -RR Attach here and now.
It might sound dramatic, but it just means that it will do what you told it to do, no questions asked.
This is how the command looks for me with KeePassC:
$ screen -D -RR -S keepassc -t KeePassC sh -c "keepassc"'
This means that I can always run this one command and it will always do the same thing; if there’s already a session created it will then detach that session (if attached anywhere) and then attach the session to the terminal I ran the command from.
The default title for a new screen looks like this:
With a few tweaks you can get it to look like this:
KeePassC [Screen 0 at Atlas]
If you want equally fancy titles, then add the following two lines to
hardstatus off hardstatus string "%t [Screen %n at %H]"
You can then choose your own title with the flag
-t <your title> as seen in the example above in the previous part.