{"id":7290,"date":"2024-09-03T01:16:00","date_gmt":"2024-09-03T05:16:00","guid":{"rendered":"https:\/\/www.both.org\/?p=7290"},"modified":"2024-09-02T21:13:14","modified_gmt":"2024-09-03T01:13:14","slug":"using-the-screen-command-for-flexibility","status":"publish","type":"post","link":"https:\/\/www.both.org\/?p=7290","title":{"rendered":"Using the screen command for flexibility"},"content":{"rendered":"<div class=\"pld-like-dislike-wrap pld-template-1\">\r\n    <div class=\"pld-like-wrap  pld-common-wrap\">\r\n    <a href=\"javascript:void(0)\" class=\"pld-like-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"7290\" data-trigger-type=\"like\" data-restriction=\"cookie\" data-already-liked=\"0\">\r\n                        <i class=\"fas fa-thumbs-up\"><\/i>\r\n                <\/a>\r\n    <span class=\"pld-like-count-wrap pld-count-wrap\">    <\/span>\r\n<\/div><\/div>\n<p>You might at first think of \u201cscreen\u201d as the device on which your Linux desktop is displayed. That is one meaning. For SysAdmins like us, <strong>screen<\/strong> is a program, a screen manager, that enhances the power of the command line. The Screen utility allows launching multiple shells in a single terminal session and provides means to navigate between the running shells.<\/p>\n\n\n\n<p>If you&#8217;re having a bit of confusion regarding the terminology of the command line, my recent article, <a href=\"https:\/\/www.both.org\/?p=7030\" data-type=\"link\" data-id=\"https:\/\/www.both.org\/?p=7030\" target=\"_blank\" rel=\"noreferrer noopener\">The Linux Philosophy for SysAdmins, Tenet 05 \u2014 Embrace the CLI<\/a>, has a good explanation of the terms you&#8217;ll encounter. I suggest you read that article first and then return here to continue.<\/p>\n\n\n\n<p><em><strong>Note:<\/strong> The term, Screen, with uppercase S refers to the screen program as a tool, while <strong>screen<\/strong>, in lowercase and bold refers to the command.<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The problems<\/h2>\n\n\n\n<p>I have many times had a remote session running a program when the communications link failed. When that happened the running program was terminated as well and I had to restart it from the beginning. It could get very frustrating. The Screen program can prevent that. The remote Screen session will continue to run even if the connectivity to the remote host is broken because the network connection fails. It also allows the intentional disconnection of the Screen session from the terminal session and reconnecting later from the same or a different computer. All of the CLI programs running in the screen terminal sessions will continue to run on the remote host. This means that once communications is reestablished one can log back into the remote host, use the <strong>screen -r<\/strong> command at the remote command line to reattach the screen session to the terminal.<\/p>\n\n\n\n<p>Sometimes I&#8217;ve been working on a remote host and had the need to leave it running while I move to another location. Terminating an SSH network connection to a remote host also terminates the program running on that remote host. I usually start up one or more terminal sessions in Screen, disconnect from Screen and log out of the remote host. Then I can go to another location, login to a different host, SSH to the host running my programs in screen, login and use the screen -r command to reconnect to the screen session and all of the terminal sessions and their respective programs will still be running.<\/p>\n\n\n\n<p>There are also some environments where physical access to a hardware console is not available to provide access to the Virtual Consoles but the flexibility of multiple shells is needed via a remote connection like SSH.<\/p>\n\n\n\n<p>In other cases, a GUI desktop is not available and the options for multiple terminal sessions or use of a GUI terminal emulator is not possible. In this situation you can use multiple Virtual Consoles, but that can still be constraining. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Screen<\/h2>\n\n\n\n<p>You will probably find it convenient to use the Screen program in many of those cases in order to work quickly and efficiently. Let&#8217;s look at how Screen can help. But first install the screen program. I use DNF because it&#8217;s the CLI software management tool for Fedora, my distro. You can use the tool for your distro. Many distros have graphical software tools.<\/p>\n\n\n\n<p>As root, install the Screen package.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># <strong>dnf -y install screen<\/strong><\/code><\/pre>\n\n\n\n<p><em><strong>Usage Note: <\/strong>In this experiment I provide instructions such as \u201cpress <strong>Ctrl-a + c<\/strong>\u201d to open a new terminal, for example. That means that you should hold down the Control key while you press the \u201ca\u201d key; at this point you can release the Control and \u201ca\u201d keys because you have alerted the Screen program that the next keystroke is intended for it. Now press the \u201cc\u201d key. For the sequence Ctrl-a + &#8221; (double quote) sequence which shows a list of all open terminals in that screen session, do Ctrl-a, release those keys and then press shift plus the &#8221; (double quote) at the same time. Use the Ctrl-a + Ctrl-a sequence which toggles between the most recent two terminal sessions. You must continue to hold down the Control key and press the \u201ca\u201d key twice. This sequence of keystrokes seems a bit complicated but I soon learned it as muscle memory and it is quite natural by now.<\/em><\/p>\n\n\n\n<p>Perform this experiment in a terminal session as a non-root user. Any terminal emulator will work. Enter the <strong>screen<\/strong> command which will clear the display and leave you at a command prompt. You are now in the screen display manager with a single terminal session open and displayed in the window.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ <strong>screen<\/strong><\/code><\/pre>\n\n\n\n<p>You won&#8217;t see much change as there&#8217;s no indicator to tell you that the screen program is running. This is one of its best features; it just gets out of the way and lets you work. <\/p>\n\n\n\n<p>Type any command such as ls to have something displayed in the terminal session besides the command prompt. You could use <code><strong>ls -l <\/strong><\/code>which is one I use just to see something in the screen session.<\/p>\n\n\n\n<p>Press <strong>Ctrl-a + c<\/strong> to open a new shell within the screen session and enter a different command, such as<strong> df -h<\/strong> in this new terminal session. Type <strong>Ctrl-a + a<\/strong> to easily switch between the two terminal sessions. Enter <strong>Ctrl-a + c<\/strong> to open a third terminal. Now type <strong>Ctrl-a + &#8220;<\/strong> to list the open terminals. <\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><a href=\"https:\/\/www.both.org\/wp-content\/uploads\/2024\/09\/Screen-01-Ctrl-a-dblqt.png\" target=\"_blank\" rel=\"noreferrer noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"903\" height=\"765\" src=\"https:\/\/www.both.org\/wp-content\/uploads\/2024\/09\/Screen-01-Ctrl-a-dblqt.png\" alt=\"\" class=\"wp-image-7304\" style=\"width:661px;height:auto\"\/><\/a><figcaption class=\"wp-element-caption\">Figure 1: Use the key combination <strong>Ctrl-a + &#8220;<\/strong> to display a list of open terminal windows in the screen session. Use the Up\/Down arrow keys to select the desired terminal and press Enter. Click to enlarge.<\/figcaption><\/figure>\n\n\n\n<p>Choose window 1 &#8212; session numbering starts at zero ( 0 ) &#8212; by using the up\/dn arrow keys and hit the <strong>Enter<\/strong> key to switch to that terminal. To close the active terminal, type <strong>exit<\/strong> and press the <strong>Enter<\/strong> key.<\/p>\n\n\n\n<p>Type the command <strong>Ctrl-a + &#8220;<\/strong> to verify that the terminal is gone. Notice that the terminal with the number you have chosen to close is no longer there and that the other terminals have not been renumbered. To reopen a fresh terminal use <strong>Ctrl-a + c<\/strong>. This new terminal is assigned the lowest available number.<\/p>\n\n\n\n<p>Type <strong>Ctrl-a <\/strong><strong>+ <\/strong><strong>&#8220;<\/strong> to verify that the new terminal has been created. Notice that it has been opened in the place of the terminal that was previously closed.<\/p>\n\n\n\n<p>To disconnect from the screen session and all open terminals, press <strong>Ctrl-a <\/strong><strong>+ <\/strong><strong>d<\/strong>. Note that this leaves all of the terminals and the programs in them intact and still running.<\/p>\n\n\n\n<p>Enter the command <strong>screen -list<\/strong> command on the command line to list all of the current screen sessions. the <strong>screen -ls<\/strong> command does the same thing and is a bit shorter. Either way, this can be useful to ensure that you reconnect to the correct screen session if there are multiple ones.<\/p>\n\n\n\n<p>Use the command <strong>screen \u2013r<\/strong> to reconnect to the active screen session. If multiple active screen sessions are open, then a list of them will be displayed and you can choose the one to which you wish to connect; you will have to enter the name of the screen session to which you want to connect.<\/p>\n\n\n\n<p>I recommend against opening a new screen session inside of an existing screen session. It can be difficult to switch between the terminals because the screen program does not always understand which of the embedded sessions to which to send the command.<\/p>\n\n\n\n<p>I use the screen program all the time. It is a powerful tool that provides me with extreme flexibility for working on the command line.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You might at first think of &ldquo;screen&rdquo; as the device on which your Linux desktop is displayed. That<\/p>\n","protected":false},"author":2,"featured_media":4636,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[100,5],"tags":[551,552],"class_list":["post-7290","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-command-line","category-linux","tag-screen","tag-the-screen-program"],"modified_by":"David Both","_links":{"self":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/7290","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7290"}],"version-history":[{"count":16,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/7290\/revisions"}],"predecessor-version":[{"id":7317,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/7290\/revisions\/7317"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/media\/4636"}],"wp:attachment":[{"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7290"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7290"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7290"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}