A GNU/screen can be sometimes to much of software for a specific purpose. For (de)taching terminal sessions there is a alternative tool called dtach. programs can be detached - run in the background - and then later reattached. dtach serves exactly for that one particular purpose. Running task while being AFK.
dtach has one big difference compared to running screen. The session naming syntax depends dtach using a UNIX socket. Socket connection name is automatically the dtach session name. Create a new dtach session. Naming the session is mandatory. No session name? No dtach session. It is as easy as that.
This is written in the dtach man file about sessions and Unix-domain sockets:
... Sessions are represented by Unix-domain sockets in the filesystem. No other permission checking other than the filesystem access checks is performed. dtach creates a master process that monitors the session socket, the program, and any attached terminals. ...
In the example below, it is explained how to start /bin/bash using dtach. BASH is used in this example to distinguish between inside dtach and outside of it
This is the syntax of dtach:
dtach -c <socket> <options>
The -c session name is a UNIX socket, and calling -c is unconditional session creation.
A socket is created in the users home directory for the session per default if calling socket without absolute path.
The following example attaches to the ~/dtach-bash session if one exists, if a session does not exists, then creates a new session using ~/dtach-bash as the socket name for the created session.
Summary of most important dtach operating modes with a short explanation, written in the man page:
-a Attach to an existing session.
-A Attach to an existing session, or create a new one.
-c Creates a new session.
-n Creates a new session, without attaching to it.
-p Copies the contents of standard input to a session.
Explanation of the -A and -z options used with dtach directly from the man dtach page:
-A Attach to an existing session, or create a new one.
dtach first tries to attach to the session specified by <socket> if possible.
If the attempt to open the socket fails, dtach tries to create
a new session before attaching to it.
-z Disables processing of the suspend key. Normally, dtach
will suspend itself when the suspend key is pressed.
With this option, the suspend character is sent
to the session instead of being handled by dtach.
To close the session. From inside of the dtach session, detach the active session using CTRL and /
There is no native dtach tools to show running currently running sessions. But this does not mean you can not find out what is running dtach. Show running session using the linux process handling tools procps. Showing running dtach sessions using the ps tool:
user 1629 0.0 0.0 1876 896 ? Ss Jan13 0:19 dtach -A dtach-session -z bash user 15196 0.0 0.1 7312 1736 pts/6 S+ 20:13 0:00 grep dtach
Showing the running UNIX socket in the home directory of the user, it has srw, only readable by user itself, has a suffix*=*
Detach the running session, this is assumed the current directory is the users home directory ~/ AND the s socket is in that home directory ~/dtach-bash=. The user only can read and write to this UNIX socket. It is essential to use the path to attaching dtach sessions.
Relative path - assuming socket is running in users /home directory /home/user/
Absolute path - assuming socket is running in users /home directory /home/user/
The socket can be used only be this particular user, it is a private session. Existing as long as the node runs, without using TCP/IP for this, availble only locally on the node. Ending a running dtach session using either the key combo CTRL D or type in exit within the running dtach session:
dtach is build only for the purpose to run one program in a dtach-ed state if there is no daemon available. One could use GNU/screen for that but screen is already to powerful, has to many options for only this. The attack surface is bigger, because the implementation is so powerful. Using dtach is like using the UNIX philosophy at its escence.
Doug McIlroy, the inventor of Unix pipes:
(i) Make each program do one thing well. To do a new job, build afresh rather than complicate old programs by adding new features.
...
Happy dtach-ing instead of screen-ing. Give this tool some honest try out, then decide if it useful or not.